需要一些关于模板语法的建议/建议

发布于 2024-09-24 19:27:49 字数 510 浏览 5 评论 0原文

我编写了一个小型的 php 模板解析器,因此可以轻松解析模板,并且模板中的变量类似于 {variable_name} 例如,

<title>{title}</title>

您能建议我可能的 if/else 语句语法吗?

我想过做类似的事情:

{if {logged_in}: TRUE}
You're logged in...
{else}
You're not...
{/if}

并且...

{if {logged_in}: TRUE}
You're logged in...
{/if}

上面演示了基本的 if/else 模板语法(它检查变量logged_in == true),但由于我更像是一名编码员而不是设计师,所以想知道我是否可以接受您的输入(因此设计人员可以轻松理解语法,而无需了解服务器端编码)。

干杯!

I've coded a small php template parser, so templates can be easily parsed, and the variables within the templates are like {variable_name} e.g.

<title>{title}</title>

Can you suggest me possible if/else statement syntax?

I've thought of doing something like:

{if {logged_in}: TRUE}
You're logged in...
{else}
You're not...
{/if}

and...

{if {logged_in}: TRUE}
You're logged in...
{/if}

The above demonstrates basic if/else template syntax (it checks if the variable logged_in == true), but since I'm more of a coder then designer, was wondering if I can have your input (so designers can easily understand the syntax without knowledge of server-side coding).

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

小忆控 2024-10-01 19:27:49

当然。
这是:

<?if ($logged_in):?>
You're logged in...
<?else:?>
You're not...
<?endif?>

有史以来最好的语法。
如您所见,开始和结束标记略有更改。
但根本不需要解析任何东西 - 只需 include 这个文件即可!
顺便说一句,你的输出看起来像

<title><?=$title?></title>

Sure.
Here it is:

<?if ($logged_in):?>
You're logged in...
<?else:?>
You're not...
<?endif?>

Best syntax ever.
As you can see, opening and closing tags been changed slightly.
But no need to parse anything at all - just include this file!
Btw, your output would look like

<title><?=$title?></title>
谈情不如逗狗 2024-10-01 19:27:49

我建议这样做:

<title><?php echo $title ?></title>

<?php if( $logged_in ): ?>
You're logged in...
<?php else: ?>
You're not...
<?php endif; ?>

<?php if( $logged_in ): ?>
You're logged in as <?php echo $logged_in_username ?>
<?php endif; ?>

换句话说,根本不需要模板引擎,

除非您将其作为学习经验,否则不要使用模板引擎,只需直接使用 PHP

I recommend this:

<title><?php echo $title ?></title>

<?php if( $logged_in ): ?>
You're logged in...
<?php else: ?>
You're not...
<?php endif; ?>

<?php if( $logged_in ): ?>
You're logged in as <?php echo $logged_in_username ?>
<?php endif; ?>

In other words no template engine at all

Unless you're doing it as a learning experience don't bother with a template engine, just use straight PHP

世态炎凉 2024-10-01 19:27:49

通常条件语句在语法上与变量替换不同,因为它们可能有一个名为 $else 等的变量......

{% if logged_in == TRUE %}
You're logged in...
{% else %}
You're not...
{% endif %}

有道理。然后您可以遵循一些标准并将 == 替换为 >= <= != 等...而无需学习大量的新语法:)

Typically conditionals differ in syntax from the variable replacement since they could have a variable called $else etc...

{% if logged_in == TRUE %}
You're logged in...
{% else %}
You're not...
{% endif %}

Makes sense. Then you can follow some standards and replace == with >= <= != etc... without a huge new syntax to learn :)

习ぎ惯性依靠 2024-10-01 19:27:49

是的,为什么你不想使用像 Smarty 这样的成熟模板系统呢?我一直在使用它,它非常完美。

但如果你仍然想按照自己的方式去做,我认为你应该问的是开发人员,而不是设计师。设计人员通常只给您 html 文件(有时只是一个 Photoshop 文件),而不想被变量、控制结构等所困扰。开发人员希望并需要 IF 内的复杂语句,例如函数和逻辑语句。

不过,您的语法看起来很合乎逻辑,并且您可以使用它。

Yes, why wouldn't you want to use something like Smarty, which is a mature templating system? I use it all the time and it's perfect.

But if you still want to do it your way, I think the developers are the ones who you should be asking, not designers. Designers usually just hand you the html files (sometimes just a Photoshop file) and don't want to be bothered with variables, control structures, etc. And developers want and need complex statements inside IFs, like functions and logical statements.

Your syntax looks logical, though and you can use it.

请帮我爱他 2024-10-01 19:27:49

对于那些说应该避免使用模板系统的人来说,模板有很多优点。例如,假设您制作了一个功能丰富的巨大套件(论坛软件、配置文件、动态用户内容、作品),并且您希望人们能够编辑信息的外观。如果您给他们直接基于 PHP 的访问权限,他们可能很容易破坏某些内容并使您的脚本不再编译。他们还可以注入 PHP 来获取隐藏在代码中的变量或完全使您的软件崩溃。使用模板限制了它们可以做的事情,因此也限制了它们可以破坏的事情。

我没有对模板做过很多工作,但根据我的经验,任何表达式通常都具有与变量不同的封装。例如:

value_which_should_be_true 是真的!哦,还有{信息}

value_which_should_be_true 不正确...哦,还有 { different_information}

这样你就可以使用 preg_replace 来替换 \{([A-Za-z0-9_-]+)}\ 和 $values[$1] 的值($1 是 preg_replace 的匹配项)作为值和唯一的东西你必须 exec() 位于注释块内。

To anyone who said that you should avoid a template system, there are a multitude of advantages to template. For instance, suppose you make a huge feature-rich suite (forum software, profiles, dynamic user content, the works) and you want people to be able to edit the appearance of the information. If you give them direct PHP-based access, they could very easily break something and make your script no longer compile. They could also inject PHP to get at variables hidden within your code or to completely bring your software to its knees. Using templates limits what they can do and therefore it limits what they can break.

I haven't done a lot of work with templates, but in my experience any expressions usually have a different encapsulation from variables. So for instance:

<!-- IF {value_which_should_be_true} -->

value_which_should_be_true is true! Oh, and {information}

<!-- ELSE -->

value_which_should_be_true wasn't true... oh, and {different_information}

<!-- ENDIF -->

This way you can just use preg_replace for \{([A-Za-z0-9_-]+)}\ with the value of $values[$1] ($1 being the match from the preg_replace) for the values and the only thing that you have to exec() is inside of the comment blocks.

不爱素颜 2024-10-01 19:27:49

在大括号中,您可以放置​​要计算的表达式

{if logged_in == TRUE}
  You're logged in...
{else}
  You're not...
{endif}

当您解析模板时,您可以在大括号中计算表达式

{if foo == 10 + bar }
  do something
{else}
  do something else
{endif}

您还可以考虑将单大括号更改为双大括号,因为它不太可能出现在模板文件中。

in braces you can put expressions which be evaluated

{if logged_in == TRUE}
  You're logged in...
{else}
  You're not...
{endif}

When you parse template you can evaluate expressions in bracess

{if foo == 10 + bar }
  do something
{else}
  do something else
{endif}

You may also consider to change single braces to double because it's less likely to appear in your template file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文