Codeigniter template class: check if pseudo variab

2019-08-17 01:33发布

Not sure if it's possible, but can you use if statements inside a template?

So if a phone number does not have a value I don't want to display that sentence at all...

<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset="utf-8"/>
    <title>{form_title}</title>

</head>


<body>
    <p>You received the following message from {name} through the Gossip Cakes' contact form.</p>

    <p>Their email is {email}</p>

    <p>Their phone number is {phone}</p>

    <p>The message: {message}</p>

</body>


</html>

I guess I could use straight php, but is there a method of returning the html of a view?

3条回答
Animai°情兽
2楼-- · 2019-08-17 01:42

class MY_Parser extends CI_Parser{

/**
 *  Parse a template
 *
 * Parses pseudo-variables contained in the specified template,
 * replacing them with the data in the second param, 
 * and clean the variables that have not been set
 *
 * @access  public
 * @param   string
 * @param   array
 * @param   bool
 * @return  string
 */
function _parse($template, $data, $return = FALSE)
{
    if ($template == '')
    {
        return FALSE;
    }

    foreach ($data as $key => $val)
    {
        if (is_array($val))
        {
            $template = $this->_parse_pair($key, $val, $template);
        }
        else
        {
            $template = $this->_parse_single($key, (string)$val, $template);
        }
    }

    #$template = preg_replace('/\{.*\}/','',$template);
    $patron = '/\\'.$this->l_delim.'.*\\'.$this->r_delim.'/';
    $template = preg_replace($patron,'',$template);
    if ($return == FALSE)
    {
        $CI =& get_instance();
        $CI->output->append_output($template);
    }

    return $template;
}

}

Enjoy :D

查看更多
beautiful°
3楼-- · 2019-08-17 01:51

Assuming you're using CI's built in parser, you have to prep all the variables beforehand. There is no support for conditions, variable assignment, or anything beyond loops and basic token replacement at this time.

To do this in CI, you'd have to prep the whole message, something like this in your controller:

if ($phone) {
    $data['phone_msg'] = "<p>Their phone number is $phone</p>";
} else {
    $data['phone_msg'] = '';
}

Not a good solution. Personally I'd recommend Twig if you're looking for a nice template parser. Your idea of "I guess I could use straight PHP" is also a very good one.

is there a method of returning the html of a view?

Use the third param of view() like so:

$html = $this->load->view('myview', $mydata, TRUE);
echo 'Here is the HTML:';
echo $html;
// OR...
echo $this->parser->parse_string($html, NULL, TRUE);
查看更多
太酷不给撩
4楼-- · 2019-08-17 01:58

CI templates do have support for IF statements if you are creative. I use them frequently. You can even use them to prevent the template elements from being parsed if there is no data.

Consider these three example arrays:

$phone_numbers = array( 
                        array('phone_number'=>'555-1212'), 
                        array('phone_number'=>'555-1313') 
                      )

$phone_numbers = array()

$phone_numbers = array( array('phone_number'=>'555-1414') )

And consider this is the relevant section in your html:

{phone_numbers} <p>{phone_number}</p> {phone_numbers}

Using the three arrays, the respective output arrays one and three are as follows. (Using array two would print nothing as the control array is empty.)

<p>555-1212</p><p>555-1313</p>

<p>555-1414</p>
查看更多
登录 后发表回答