【问题标题】:Chaining smarty templates and clean multiline strings - PHP链接 smarty 模板和清理多行字符串 - PHP
【发布时间】:2012-10-08 10:13:31
【问题描述】:

我知道第一部分是主观的,但我想听听人们使用的一些不同的技术。这是一个由两部分组成的问题:您在 PHP 中使用什么来处理复杂的多行字符串?还有,我可以使用 smarty 的组合类型的关系吗?

问题 1:我知道有 heredoc 和 "."操作员。如果有的话,我正在寻找新鲜的、更易读的想法。

问题 2:更具体地说,这是我想对 smarty 做的事情。

假设我有一个模板,base.tpl:

<html>
<head><title></title></head>
<body>
{$main_content}
</body>
</html>

我可以链接模板,即代表 $main_content 的另一个模板,比如 main.tpl:

<div id="header">$header</div>
<div id="container">
<h1>{$first_header}</h1>
<p>{$first_paragraph}</p>
<h1>{$second_header}</h1>
<p>{$second_paragraph}</p>

我想在whatever.php中将一个模板加载到另一个模板中,即:

// ... including smarty and all other boiler plate things ...

$smarty -> assign('first_header', "Foo");
$smarty -> assign('first_paragraph', "This is a paragraph");
$smarty -> assign('second_header', "Bar");
$smarty -> assign('second_paragraph', "This is another paragraph");

$main_content = $smarty->load('main.tpl');
$smarty -> display('base.tpl');

我知道smarty里面有“模板继承”,但是不熟悉。它能给我类似的功能吗?

注意:我认为 heredoc 最大的问题是我无法为 html 获得语法高亮(如果我在 heredoc 字符串中指定 html)。如果没有突出显示,我想通过 smarty 传递的 html 会更难阅读,这有悖于 smarty 的目的。

【问题讨论】:

  • 您的模板看起来无效....似乎缺少{} ???
  • 对不起,我只是通过一个例子,我是smarty的新手。不过我会修复它
  • 我的回答在模板中包含模板是否有意义?
  • 是的,这或多或少是我想要的。谢谢

标签: php smarty multilinestring


【解决方案1】:

今天早上我找到了一些关于模板继承的文档...

为了扩展我上面的例子,你可以有一个基本布局(base.tpl)

<html>
<head><title>{$title|Default="title"}</head>

<body>

<nav>{block name=nav}{/block}</nav>

<div id="main">{block name=main}Main{/block}</div>

<footer>Copyright information blah blah...</footer>

</body>
</html>

您现在可以使用新版本的 smarty (home.tpl) 扩展模板并覆盖块

{extends file=base.tpl}

{block name=nav}
          <ul style="list-style:none">
            {foreach $links as $link}
              <li><a href="{$link.href}" target="_blank">{$link.txt}</a></li>
            {/foreach}
          </ul>
{/block}

{block name=main}
    {foreach $paragraphs as $p}
        <h2>{$p.header}</h2>
        <p>{$p.content}</p>
    {/foreach}
{/block}

http://www.smarty.net/inheritance

【讨论】:

    【解决方案2】:

    您需要使用 {include} 在模板中调用模板(片段)。

    http://www.smarty.net/docsv2/en/language.function.include.tpl

    <html>
    <head>
      <title>{$title}</title>
    </head>
    <body>
    {include file='page_header.tpl'}
    
    {* body of template goes here, the $tpl_name variable
       is replaced with a value eg 'contact.tpl'
    *}
    {include file="$tpl_name.tpl"}
    
    {include file='page_footer.tpl'}
    </body>
    </html>
    

    将变量传递到包含的模板中:

    {include file='links.tpl' title='Newest links' links=$link_array}
    {* body of template goes here *}
    {include file='footer.tpl' foo='bar'}
    

    在多行字符串方面,我倾向于使用这种模式:

    $my_string = "Wow, this is going to be a long string. How about "
               . "we break this up into multiple lines? "
               . "Maybe add a third line?";
    

    正如你所说,这是主观的。无论你觉得舒服,只要它易于阅读......

    【讨论】:

      猜你喜欢
      • 2015-05-09
      • 2017-02-14
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 2016-08-05
      • 2011-01-25
      • 1970-01-01
      • 2019-02-08
      相关资源
      最近更新 更多