无脂肪框架[F3]不会取代@,我缺少什么?

时间:2017-12-02 14:36:13

标签: php templates fat-free-framework

我可以这样替换字符串:

<?php echo $f3->get('Hoteles'); ?>

但我可以这样:

{{ @Hoteles }}

输出是{{@Hoteles}}我感到愚蠢和沮丧。但是无法发现我所缺少的东西。 ModReWrite为ON。如果我解决了这个问题,我可以使用详细的模板。

希望你能帮助我。

htaccess的:

RewriteRule ^(tmp)\/|\.ini$ - [R=404]

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L,QSA]

MAMP Pro 配置:

enter image description here

所以在 index.php 中我有这个:

<?php
$f3=require('lib/base.php');
$f3->config('config.ini');
$f3->route('GET /',
function($f3) {

    //Set Lang Path
    $f3->set('LOCALES','ui/lang/');
    $f3->set('FALLBACK','en');

    //Set Home at the beginning.
    $f3->set('content','home.htm');
    //See Home inside main Layout
    echo View::instance()->render('layout.htm');

    $view = new View;
    echo $view->render('templates/header.htm');
    echo $view->render('templates/footer.htm');

}
);
$f3->run();

layout.htm 中我有:

<body>
    <?php echo $this->render(Base::instance()->get('content')); ?>
</body>

home.html 中,我有:

<?php
      $f3=Base::instance();
 ?>


<!--  card -->
      <div class="card border-light mb-3 mr-1">
        <div class="card-header"><?php echo $f3->get('Hoteles'); ?></div>
        <div class="card-body">
          <h4 class="card-title"><?php echo $f3->get('GuiayPrecios'); ?></h4>
          <p class="card-text"><?php echo $f3->get('DescubraHoteles'); ?></p>
        </div>
      </div>
<!--  End card -->

而不是<?php echo $f3->get('Hoteles'); ?>我想要使用:{{ @Hoteles }} 因为这个想法是在模板中重用html组件。例如,可以使用<repeat>

迭代CARD

1 个答案:

答案 0 :(得分:3)

简答:

使用PreviewTemplate类代替View

答案很长:

Fat-Free Framework提供3种不同的模板类:

1)查看

This class使用普通的PHP作为模板引擎。参见:

// controller.php
$f3->data = [
  'name' => 'Fat-Free Framework',
  'tags' => ['PHP','framework','lightweight'],
];
$view = View::instance();
echo $view->render('template.php');
<!-- template.php -->
<?= $data['name'] ?> has the following tags:
<ul>
  <? foreach ($data['tags'] as $tag): ?>
    <li><?= $tag ?></li>
  <? endforeach; ?>
</ul>
<a href="<?= Base::instance()->alias('home') ?>">Return to homepage</a>

2)预览

This class使用非常接近PHP的语言作为模板引擎,允许使用类似Javascript的数组表示法,filters和模板字符串compilation

PHP开头和结尾标记被{~~}替换,PHP短标记被{{}}替换:

// controller.php
$f3->data = [
  'name' => 'Fat-Free Framework',
  'tags' => ['PHP','framework','lightweight'],
];
$preview = Preview::instance();
echo $preview->render('template.tpl');
<!-- template.tpl -->
{{ @data.name }} has the following tags:
<ul>
  {~ foreach (@data.tags as @tag): ~}
    <li>{{ @tag }}</li>
  {~ endforeach; ~}
</ul>
<a href="{{ 'home' | alias }}">Return to homepage</a>

3)模板

This class为XML样式的模板提供引擎。由于此类扩展了Preview class,因此它支持类似Javascript的数组表示法,filters和模板字符串compilation。此外,它还支持类似XML的指令,这些指令带来了loopsconditional segmentssubtemplate inclusion等新功能。

E.g:

// controller.php
$f3->data = [
  'name' => 'Fat-Free Framework',
  'tags' => ['PHP','framework','lightweight'],
];
$template = Template::instance();
echo $template->render('template.tpl');
<!-- template.tpl -->
{{ @data.name }} has the following tags:
<ul>
  <repeat group="@data.tags" value="@tag">
    <li>{{ @tag }}</li>
  </repeat>
</ul>
<a href="{{ 'home' | alias }}">Return to homepage</a>

渲染方法签名

请注意,这3个模板类对render()方法使用相同的签名:

string render ( string $file [, string $mime = 'text/html' [, array $hive = NULL, [ int $ttl = 0 ]]] )

这使您可以选择将模板变量存储为全局或局部变量:

全局模板变量:

$f3->data = [
  'name' => 'Fat-Free Framework',
  'tags' => ['PHP','framework','lightweight'],
];
// NB: at this stage, any other class can access $f3->data
$engine = View/Preview/Template::instance();
echo $engine->render('template.tpl');

本地模板变量:

$data=[
  'name' => 'Fat-Free Framework',
  'tags' => ['PHP','framework','lightweight'],
];
// NB: at this stage, only this part of code knows $data
$engine = View/Preview/Template::instance();
echo $engine->render('template.tpl','text/html',$data);

在小型项目上,这并没有多大区别,但是对于更大的项目,我建议将模板变量设置为本地,并保留真正需要在类/模块之间共享的数据的全局变量。