Skip to content

Template tags

Jorge Castro edited this page May 1, 2020 · 10 revisions

Basic

Using a template layer means separating the project into two parts (or 3 parts).

  • Our PHP code
  • Our View or template
  • The View compiled (it is generated automatically, so we do nothing but it is worth to know that it exists)

Our PHP code always is executed at first, and it calls the view. So the flow is always PHP -> VIEW

Let's say our code has the variable $customer="John" and we want to show in the view.

So, we could create the next code (in our PHP code)

(page.php)

$blade=new BladeOne(); // our instance to blade
echo $blade->run('folder.folder2.view',['consumer'=>$customer]);

It joins our code with the view. In this case, the view must exists in the next folder

/views/folder/folder2/view.blade.php (the prefix blade.php is mandatory)

But how could we show the variable?. Using template tags.

(/views/folder/folder2/view.blade.php )

{{$consumer}}

Where {{$variable}} is a template tag (there are many ones but you don't need to use all of them)

In our PHP code, we write the next line: "['consumer'=>$customer]", it creates the template variable $consumer with the value of $customer. Of course, both variables could have the same name.

This code is compiled as follows.

(/compiles/XXXXXXXXXXXXX.blade)

<?php echo BladeOne::_e($consumer); ?>

So the compiled code is simply vanilla PHP code. By default, the compiled file is generated once when it is required, or you could generate each time or never, so it also optimizes the execution because we don't want to generate the file per each request.

Template tags

Basic

Tag Note Example
{{$variable}} It shows a variable or evaluates a function. The result is escaped (i.e. encoding special characters) {{$country}}
{!!$variable!!} It shows a variable or evaluates a function. The result is not escaped. {!!$country!!}
{{-- text --}} It is a comment (the content inside is not evaluated) {{-- hi there --}}
@tag / @tag($arg) It calls a tag or method defined by the library @dump($variable)
@{{$variable1}} show the value of the content directly (not evaluated, useful for js) @{{$country}}
{{ $name or 'Default' }} value or default {{$country or "no country"}}
{{Class::StaticFunction($variable)}} call and show a function (the function should return a value) {{var_dump($variable)}}
{{Class::method($variable)}}