I can replace strings this way:
<?php echo $f3->get('Hoteles'); ?>
But I can't this way:
{{ @Hoteles }}
The output is {{ @Hoteles }} I feel dumb and frustrated. But cannot discover what i'm missing. ModReWrite is ON. If i solve this, i can work with elaborated templates.
Hope you can give me a hand.
.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 config:
So in index.php i have this:
<?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();
In layout.htm I have this:
<body>
<?php echo $this->render(Base::instance()->get('content')); ?>
</body>
In home.html I have this:
<?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 -->
Instead of <?php echo $f3->get('Hoteles'); ?>
I want to use: {{ @Hoteles }}
Because the idea is to re-use html components inside the template. For example that CARD can be iterated using <repeat>
Use either Preview
or Template
class instead of View
.
Fat-Free Framework offers 3 different templating classes:
This class uses plain PHP as a template engine. See:
// 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>
This class uses a language very close to PHP as a template engine, allowing the use of Javascript-like array notations, filters and template strings compilation.
PHP opening and ending tags are replaced with {~
and ~}
, and PHP short tags are replaced with {{
and }}
:
// 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>
This class provides an engine for XML-style templates. Since this class extends the Preview class, it supports Javascript-like array notations, filters and template strings compilation. Additionally, it supports XML-like directives, which bring new features such as loops, conditional segments and subtemplate 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>
Note that those 3 templating classes use the same signature for the render()
method:
string render ( string $file [, string $mime = 'text/html' [, array $hive = NULL, [ int $ttl = 0 ]]] )
This gives you the choice between storing template variables a global or local variables:
Global template variables:
$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');
Local template variables:
$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);
On small projects, that doesn't make much difference, but on bigger projects, I'd recommend setting template variables as local and keeping global variables for data that really needs to be shared among classes/modules.