Are you looking for steps to install twig on Linux? Here’s how we do it.
Here at Bobcares, we have seen several such Linux related queries as part of our Server Management Services for web hosts and online service providers.
Today we’ll see how to install twig on Linux.
Know more about twig and why is it necessary
Twig is a fast, secure, and flexible modern template engine for PHP. Also, it is a good alternative for PHPTemplate as it represents a much cleaner templating experience.
Moreover, it has easy to understand syntax and there is no need to perform dynamic PHP operations in template files.
How to install Twig on Linux
Here are the steps our Support Engineers follow to install Twig. It is recommended to install Twig via Composer which is a PHP dependency management tool.
So now first, we shall download the Composer installer into the application’s root folder (twig):
$ cd /var/www/twig
$ curl -s http://getcomposer.org/installer | PHP
This will download a single file named composer.phar which is the Composer executable. Next, create a file named composer.json right next to it.
$ nano composer.json
Then paste the below lines into the file.
{ “require”: { “twig/twig”: “1.*” } }
This will make our application to be dependent on the Twig templating engine. Now to install the dependencies, let’s use the composer command.
$ php composer.phar install
The Twig files get downloaded into the vendor/Twig folder. Then we need to connect it to the application. Since we’ve used the Composer, we can use its autoloader functionality to load all the dependencies that we installed. So let us open the app.php file.
$ nano app/app.php
Now remove all the contents within it and add the below line at the top.
<?php require_once '../vendor/autoload.php'; ?>
Using this line, we can load the Composer autoloader which in turn loads Twig. In case, if you’ve installed the Twig using other methods then you will have to require the Twig autoloader directly.
require_once '/path/to/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
However, we don’t need this with the Composer. Below the required statement we just wrote, we need to locate a Twig template file and pass the variable that we want to print out to the page. So paste the below code.
<?php $loader = new Twig_Loader_Filesystem('../views/'); ?> <?php $twig = new Twig_Environment($loader); ?> <?php echo $twig->render('page.html', array('text' => 'Hello world!')); ?>
Now we shall create a page.html file and inside it we will have a variable available that contains the statement we want print.
$ nano views/page.html
Paste the below code within it.
<h1>{{ text }}</h1>
After saving the file, if you load the app.php file in the browser, you must see the same text being printed.
Now we have this separate file and we can display it however we want. Also, the variables we passed through the render function are now available to be printed in between the double curly braces.
Finally, now you can experiment with this file however you want. Moreover, you can pass multiple variables to the template, use them in control structures, process them through templating functions, or even apply Twig filters\
[Need any further assistance with Linux queries? – We are here to help you.]
Conclusion
In today’s writeup, we saw how our Support Engineers install twig on Linux.
0 Comments