googlenginx

Get started with Smarty

The Smarty template engine is not installed by default in Bitnami installations using system packages. However, it can be easily enabled using Composer. In this guide, you will learn how to install Smarty project.

Installation

NOTE: This section assumes that the application project directory will be located at /opt/bitnami/projects/APPNAME. Remember to replace the APPNAME placeholder with the application name, such as myapp.

  • First, create a project directory where to store your application. Run the following commands:

    $ sudo mkdir -p /opt/bitnami/projects/APPNAME
    $ sudo chown $USER /opt/bitnami/projects/APPNAME
    
  • Create a sample Smarty project. Follow these steps:

    • Create required directories to be used by Smarty, and ensure the directories used for caching and template compilation can be written to by the web server:

      $ cd /opt/bitnami/projects/APPNAME
      $ mkdir templates configs public templates_c cache
      $ sudo chown daemon:daemon templates_c cache
      
    • Create the /opt/bitnami/projects/APPNAME/public/index.php file with the following contents:

      <?php
      
      // Load Smarty installed via Composer
      require_once('../vendor/autoload.php');
      Smarty_Autoloader::register();
      
      $smarty = new Smarty();
      
      // Configure Smarty
      $smarty->setTemplateDir('../templates');
      $smarty->setCompileDir('../templates_c');
      $smarty->setCacheDir('../cache');
      $smarty->setConfigDir('../configs');
      
      // Define a sample variable to be used by the Smarty template
      $smarty->assign('name', 'Alice');
      
      // Render the Smarty template
      $smarty->display('index.tpl');
      
    • Create a sample Smarty template file at /opt/bitnami/projects/APPNAME/templates/index.tpl:

      <html>
        <head>
          <title>Smarty</title>
        </head>
        <body>
          Hello, {$name}!
        </body>
      </html>
      
  • Install Smarty to your project. Run the following command:

    $ cd /opt/bitnami/projects/APPNAME
    $ composer require smarty/smarty
    
  • Create the NGINX configuration for your application. Follow these steps:

    • Create the /opt/bitnami/nginx/conf/server_blocks/APPNAME-server-block.conf file containing the application’s HTTP server block configuration with the configuration block shown below:

      server {
          # Port to listen on, can also be set in IP:PORT format
          listen 80 default_server;
          root /opt/bitnami/myapp/public;
          # Catch-all server block
          # See: https://nginx.org/en/docs/http/server_names.html#miscellaneous_names
          server_name _;
          include  "/opt/bitnami/nginx/conf/bitnami/*.conf";
      }
      
    • Create the /opt/bitnami/nginx/conf/server_blocks/APPNAME-https-server-block.conf file containing the application HTTPS server block configuration with the configuration block shown below:

      server {
          # Port to listen on, can also be set in IP:PORT format
          listen 443 ssl default_server;
          root /opt/bitnami/myapp/public;
          # Catch-all server block
          # See: https://nginx.org/en/docs/http/server_names.html#miscellaneous_names
          server_name _;
          ssl_certificate      bitnami/certs/server.crt;
          ssl_certificate_key  bitnami/certs/server.key;
          include  "/opt/bitnami/nginx/conf/bitnami/*.conf";
      }
      
  • Finally, restart the NGINX server:

    $ sudo /opt/bitnami/ctlscript.sh restart nginx
    

You can now verify that the example application is working by visiting its URL using your browser at http://SERVER-IP/. You should see the text Hello, Alice!

More Information

Learn more about developing applications with Smarty at https://www.smarty.net/quick_install.

Last modification July 29, 2020