smarty template engine - smarty

I have an webpage written using php but now need to convert them to smarty template engine.
I am rookie to smarty template engine.I find it difficult of smarty.net document.Is there any book or site from where i can learn smarty.
I need a small snippet for insert function.

I think the smarty.net documentation is the very best point to start using Smarty Template Engine.
Most tutorials are based on the documentation I think.
For a very quick start you just have to require the Smarty base class:
// Require base class
require_once('Smarty.class.php');
// create new Smarty instance
$smarty = new Smarty();
// define Smarty directories
$smarty->template_dir = '/web/www.example.com/guestbook/templates/';
$smarty->compile_dir = '/web/www.example.com/guestbook/templates_c/';
$smarty->config_dir = '/web/www.example.com/guestbook/configs/';
$smarty->cache_dir = '/web/www.example.com/guestbook/cache/';
// define a template variable, which will be shown in your template
$smarty->assign('greeting', 'Hello World!');
// force your php script to show your template file
$smarty->display('template.html');
Thats everything you need in your PHP file.
In the default configuration of Smarty you can show your template variable greeting by using
My greeting message: {$greeting}
If you open your PHP File via a browser you will see: My greeting message: Hello World!
For further information you really should read the official Smarty documentation!

Related

Trying to implement FPDF with all forms of instantiation but only one form works. Others give Error

Installed crabbley/fpdf-laravel as per instructions. Tried some sample code as follows:
$pdf= app('FPDF');
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Swordsmen Class Times');
$pdf->Output();
While the instantiation of fpdf is different from the samples in the tutorials, all works as expected and the pdf is displayed in the browser. I got this working sample from the crabbley packagist.org/packages/crabbly/fpdf-laravel readme under 'usage'. The 'usage' instructions also provide an alternative instantiation viz: $pdf = new Crabbly\FPDF\FPDF;
The tutorial samples use something slightly different again, ie
require('fpdf.php');
x=new FPDF();
and thus are a little different. When I changed it to be the same as the tutorial, all I changed was the instantiation line from
$pdf= app('FPDF');
to
$pdf = new FPDF('L', 'mm','A4');
and I get the error 'Class 'App\Http\Controllers\FPDF' not found'. I do not understand the difference between the different forms of instantiation and not sure what is going on but I need the latter format so I can set page orientation etc. I also tried the usage format as described above with the same sort of error, ie new Crabbly\FPDF\FPDF not found.
I have tried the require statement but FPDF is not found and I am unsure where to point 'require' to.
Installation consisted of:
composer require crabbly/fpdf-laravel
add Crabbly\FPDF\FpdfServiceProvider::class to config/app.php in the providers section
Any suggestions will be appreciated.
You are using an implementation for the Laravel framework that binds an instance of FPDF to the service container. Using app('FPDF') returns you a new instance of FPDF, which is pretty much the same what new FPDF() would do.
The require way of using it is framework agnostic and would be the way to use FPDF if you are just using a plain PHP script. While you could use this way with Laravel too, why would you want to do that?
The reason the require does not work, by the way, is that the fpdf.php file is not found from where you call it. It would be required to sit in the same directory unless you give it a path. Considering you installed it using composer, the fpdf.php script, if any, should sit inside the vendor directory.
However, just go with using the service container. The line $pdf = new FPDF('L', 'mm','A4'); just creates a new instance of the FPDF class and initializes it by passing arguments to the constructor, these being 'L' for landscape orientation, 'mm' for the measurement unit, and 'A4' for the page size. Without knowing the package you use and having testing it, you should also be able to set them equivalently by calling:
$pdf = app('FPDF', ['L', 'mm', 'A4']);
Hope that helps!

Dynamically embed inline-CSS to head using Prestashop module

How can I dynamically embed an inline-CSS that was entered via the back office. There're only two register methods available for registering assets in Prestashop.
public function hookDisplayHeader()
{
$this->context->controller->registerJavascript('id', 'path/to/file.js');
$this->context->controller->registerStylesheet('id', 'path/to/file.css');
}
The registerStylesheet method has an option called inline, but that requires the content to be in an existing file first. While I have the CSS code stored in the configurations.
And trying to echo the content in hookDisplayHeader() results in it being printed above the <html> open tag.
Create a TPL file in the hooks directory of your module.
Then get the CSS code value from configurations in hookDisplayHeader() function and pass it to smarty:
$custom_css = `GET CUSTOM CSS FROM CONFIG`;
$this->smarty->assign('yourcustomcss', $custom_css);
return $this->display(__FILE__, 'dummy_file.tpl');
Then in the TPL file: dummy_file.tpl
{if isset($yourcustomcss)}
<style>{$yourcustomcss nofilter}</style>
{/if}
The TPL file is supposed to be in the following directory:
/yourmodule/views/templates/hook/dummy_file.tpl

Smarty value in value

How to run like this code
$smarty->assign('value','foo');
$smarty->assign('value1','some smarty code with {$value}');
In the template, if I use {$value}, there can't show value.
You can do this as below in PHP:
$smarty->assign('value','foo');
$smarty->assign('value1',"some smarty code with {$smarty->getTemplateVars('value')}");
Then in Smarty template file:
{$value}
{$value1}
Both variables are visible and working in Smarty template file.
However I've never used such functionality. I would rather do it this way:
$fooVariable = 'foo';
$smarty->assign('value',$fooVariable);
$smarty->assign('value1','some smarty code with {$fooVariable}');

Variable autoescape in Smarty templates

I have recently found out that Smarty, differently from Django template engine, does not escape variables automatically and I need to put |escape next to most of the variables in my templates.
Following the docs, http://www.smarty.net/docsv2/en/variable.default.modifiers.tpl I need to set default modifiers, needn't I?
So, here's my code:
$smarty = new Smarty();
$smarty->default_modifiers = array('escape:"htmlall"');
... and still variables ARE NOT escaped until I add |escape next to them.
What am I doing wrong?
If you are on Smarty 3, try this:
$smarty = new Smarty();
$smarty->loadFilter(Smarty::FILTER_VARIABLE, "htmlentities");
Tadà!
Update: Smarty::FILTER_VARIABLE is undocumented as of 28/11/2014. Use $smarty->escape_html = true if you want to stick to offical docs.
It appears that this feature was removed from Smarty v3, and docs are outdated. See:
http://www.smarty.net/forums/viewtopic.php?p=62207
I'd recommend a workaround - which is template level. Either create a new style v3 function to take care of filtration, or, do a simple include.
Include method
Put this in a clean.tpl file:
{$text|escape:htmlall}
Then invoke as {include file=clean.tpl text=$myvariabletofilter}
Function method
The new functions in Smarty could also take care of that:
{function clean}
{$text|escape:htmlall}
{/function}
And invoke as {clean text=$myvariabletofilter}
As always, make sure that these things get trimmed right and don't insert unncessary spaces.

render individual file in middleman

I am writing a helper and I need to get a rendered file as String.
I see that the method that I need exists in the middleman's library: http://rubydoc.info/github/middleman/middleman/Middleman/CoreExtensions/Rendering/InstanceMethods#render_individual_file-instance_method
How do I call this function from my helper class?
I tried:
require "middleman-core/lib/middleman-core/core_extensions/rendering.rb"
...
puts Middleman::CoreExtensions::Rendering::InstanceMethods.render_individual_file(filepath)
But it does not seem to find the file, any idea?
I'm not sure 3.0 beta is quite ready for primetime.
That said, it does sound like the partial method is what you're looking for.
Unless I'm missing something, the Middleman method seems like an overly-complex solution. For one of my sites I wanted to load entire text files into my templates, so I wrote this helper:
# Shortcut for loading raw text files. Obviously assumes that given file is in a valid format.
# #return [String] File contents
def load_textfile(filename)
File.read filename.to_s
end
Also, you should clarify if you are intending to use this within a template, or within Ruby code. It's not clear to me based on your question.
Here is an example of how one would use above helper:
Currently of note, Middleman is in the process of transitioning to version 4, and the conventions for loading helpers will change. The most straightforward way to define a helper is within a helper block in your config.rb file, as follows:
helpers do
# Define helper functions here to make them available in templates
end
I use Slim for templating. It really is the best. In slim you would appply helper as thus:
= load_textfile 'path'
p You can embed helper output in your page with interpolation, too: #{load_textfile 'path'}

Resources