Codeigniter模板类:检查伪变量是否有值 [英] Codeigniter template class: check if pseudo variables have a value

查看:101
本文介绍了Codeigniter模板类:检查伪变量是否有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定是否可以,但是可以在模板中使用if语句吗?

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>

我想我可以使用直的php,但是有一个方法返回视图的html?

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

推荐答案

假设您使用 CI内置的解析器,您必须预先准备所有的变量。此时不支持条件,变量赋值或任何超出循环和基本令牌替换。

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.

要在CI中执行此操作,您必须准备整个消息,这样的东西在你的控制器:

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'] = '';
}

不是一个好的解决方案。就个人而言,如果您正在寻找一个不错的模板解析器,我建议您 Twig 。你的想法我想我可以使用直接PHP也是一个非常好的。

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.


有一个方法返回html a

$html = $this->load->view('myview', $mydata, TRUE);
echo 'Here is the HTML:';
echo $html;
// OR...
echo $this->parser->parse_string($html, NULL, TRUE);

这篇关于Codeigniter模板类:检查伪变量是否有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆