Nes - A powerful template system for HTML and the Web.
use Nes; my $nes = Nes::Singleton->new('template.nhtml');
my $nes_tags = {}; $nes_tags->{'var_hello'} = 'Hello World!';
$nes->out(%$nes_tag);
Nes (No Embedded Source) a system for separating Perl executable code and the representation of the data generated in HTML using Tags are replaced with variables created in Perl.
Nes can do for you? With Nes can do the same or more than any other template system, only much faster and easier. The time is the most valuable resource.
Nes has only ten Tags to include in the HTML, and a single object with a single method to instantiate in Perl. You can start working with Nes in hours, not weeks or months as in other systems.
Transparent management of code injection and cross-site scripting. Plugins for creating secure forms, Forms and Captcha.
Object-oriented paradigm which enables you to reuse your code or use third-party libraries. Objects are the main feature that makes Nes different from other systems.
Nes is oriented at HTML and not the Perl code. Nes exported very few Perl functions, also to simplify, Nes Objects may also contain HTML, Javascript, CSS, Ajax, etc.. even PHP, or trail mix.
Full support of PHP Wrapper. Nes as Wrapper can use to add functionality to other types as HTML, or PHP to exchange information.
Support for mod_perl.
Creating and managing user sessions.
You can add functionality using plugins to Nes, you can create your own or use by third parties.
Insert parts of PHP code in Nes only have to type a tag, Nes allow the inclusion in your script templates in other languages other than Perl without writing a single line of Perl code. Moreover, most of them can contain Tags Nes.
You can insert SQL SELECT statements in your templates directly.
Nes provides configuration files to the style .htaccess you can also use for your script.
use Nes; my = Nes::Singleton->new('template.nhtml'); my $nes_tags = {}; $nes_tags->{'var_hello'} = 'Hello World!'; $nes->out(%$nes_tag);
{: NES 1.0 ('hello.cgi') :} <html> <head> <title>Nes Hello World</title> </head> <body> <b>{: $ var_hello :}</b> </body> </html>
Sample to use Nes; http://nes.sourceforge.net/
Repository http://github.com/Skriptke/nes
Nes requires configuration of you cgi-bin and .htaccess for directory of the Nes templates.
To active and configure Nes use utility: set_nes_site
Active Nes in /usr/lib/cgi-bin:
set_nes_site --cgi_dir=/usr/lib/cgi-bin
Configure Nes for directory /var/www/myweb:
set_nes_site --dir_cfg=/var/www/myweb
Configure Nes for all site:
set_nes_site --dir_cfg=/var/www
Install hello world and test examples in /var/www/test_nes:
set_nes_site --from_exam=hello_nes --to_exam=/var/www/test_nes
More help:
set_nes_site --help
* Change /usr/lib/cgi-bin and /var/www for you particular path
For configured Nes locally find you path to Nes.pm and add in iprefix in cgi_dir and from_exam options.
Active Nes in /usr/lib/cgi-bin:
set_nes_site --cgi_dir=/usr/lib/cgi-bin --iprefix=/home/USER/path-to-Nes
More help:
set_nes_site --help
* Change /usr/lib/cgi-bin and /home/USER/path-to-Nes for you particular path
AddHandler perl-nes .nhtml AddHandler perl-nes .nhtm Action perl-nes /cgi-bin/nes/dispatch.cgi <FilesMatch \.(nsql|nes.cfg)$> order deny,allow deny from all </FilesMatch>
The .nes.cfg is placed in the directory where you installed your nhtml templates, it overwrites Setting and the plugins configuration. Level rises, until the root directory of the Web, to read .nes.cfg files.
private_key value that is used to encrypt data, if not changed the script to launch a warning each time you run Nes.
tmp_dir # Temporary directory. tmp_suffix # Suffix of the temporary files. tmp_clear # If delete the temporary files or 0 and does not delete # time suffix: s: seconds, # m: minutes h: hours d: days, M: months, y: years # 24h is 24 hours. top_dir # Root directory of your site. nes_dir # Directory of Nes. nes_top_dir # Absolute path to the Nes directory. plugin_dir # Directory of the Plugin, top_dir relative. obj_dir # Directory of the Object, top_dir relative. plugin_top_dir # Absolute path to the Plugin directory. obj_top_dir # Absolute path to the Object directory. time_zone # *** unimplemented *** locale # locale, ej.: es_ES.utf8 session_prefix # Name cookie used for user session. private_key # Private key used to encrypt data. DB_base # Database name. DB_user # Database user. DB_pass # Database password. DB_driver # Database driver, default 'mysql' DB_host # Database host, default 'localhost' DB_port # Database port, default '3306' php_cline # Command line for PHP CLI php_cgi_cline # Command line for PHP CGI perl_cline # Command line for Perl shell_cline # Command line for Shell python_cline # Command line for Python max_post # Max kB. maximum size of POST. max_upload # Max kB. maximum size of the upload, 0 none tmp_upload # In big upload, memory consumption is high, # this defined from that kB. using a temporary # file in the upload, preventing these are # loaded into memory. # Plugins list separated by commas to load at startup only # for the file in the URL, in all nhtml directory, eg.: auto_load_plugin_top_first # Plugins list separated by commas to load at startup to # all files included in all nhtml directory, eg.: auto_load_plugin_all_first # Plugins list separated by commas to load at end only # for the file in the URL, in all nhtml directory, eg.: auto_load_plugin_top_last # Plugins list separated by commas to load at end to # all files included in all nhtml directory, eg.: auto_load_plugin_all_last
You can use .nes.cfg to add configuration variables to your script. We recommend using a notation that clearly distinguishes Nes variables, for example using prefixes the name of your application:
.nes.cfg private_key = pass3 myscript_var1 = 50 myscript_var2 = 60 myscript_var3 = 70
You can access these variables from the nhtml template as:
{: * cfg_myscript_var1 :}
And from Perl:
use Nes; my $nes = Nes::Singleton->new(); my $config = $nes->{'CFG'}; my $var1 = $config->{'myscript_var1'};
In the predefined variables, Nes detects the data type automatically:
# Is scalar tmp_dir = /tmp/nes
# It is an array, separated by commas auto_load_plugin_top_first = plugin1.pl, plugin1.pl
If we define our own variables with a particular data type:
# Scalar, using Perl syntax in the value. myscript_var1 $= 'scalar'
# Hash myscript_var2 %= ( 'name' => 'Jason', 'email' => 'jason\@example.com' )
# Array myscript_var3 @= ( 'one', 'two', 'three' )
We may also use Perl syntax for variables in the predefined value, taking care not to change the type:
# scalar tmp_dir $= '/tmp/nes'
# It is an array, ERROR tmp_dir %= '/tmp/nes'
# Array, separated by commas auto_load_plugin_top_first = plugin1.pl, plugin2.pl
# Array, Perl syntax auto_load_plugin_top_first @= ( 'plugin1.pl', 'plugin2.pl' )
In the predefined variables are all scalar except those allowed are comma-separated arrays. If we arrays can be useful if one of the values has a coma, or the scalar to define a space:
# we can not take as value a space like this: myscript_var1 = ' ' <- error, value is the literal, including quotation myscript_var1 = <- error, no value
# to give as value a space can do so: myscript_var1 $= ' '
# Array, the name includes a comma auto_load_plugin_top_first = plugin,1.pl, plugin,2.pl <- error
# Array, Perl syntax the name includes a comma auto_load_plugin_top_first @= ( 'plugin,1.pl', 'plugin,2.pl' )
Each variable can only occupy one line.
Is a singleton type object, return the new object or instance of this if is created previously.
Nes::Singleton->new('template.nhtml'); # returns a new instance Nes::Singleton->new('other.nhtml'); # returns same instance Nes::Singleton->new(); # returns same instance
Only require parameter if Nes is executed by CGI.
http://example.con/perl.cgi
The parameter is ignored if executed Nes by template.
http://example.con/template.nhtml
The CGI method only is provided for debug.
Singleton provides a single method: out. In the parameter we pass a copy of an associative array (%$hash) with the data we want to replace in the template.
my $nes_tags = {}; $nes_tags->{'hello'} = 'Hello World!'; $nes->out(%$nes_tag); ./. my %nes_tags; $nes_tags{'hello'} = 'Hello World!'; $nes->out(%nes_tag);
Nes instructions consist of blocks enclosed in braces, the keys to open and close a block are respectively:
{: and :}
The Tags are the word or symbol that precedes the keys to open the block. Similar to the HTML Tags. The Tag Nes syntax is:
{: TAG (parameters) [HTML] :}
TAG can be:
# Comment $ Variable * Environment variable ~ Expression sql SQL SELECT @ Data table @$ Field in a data table include include a file NES Nes & Plugin
It allows abbreviated as follows:
{: $ ( 'variable' ) :} {: $ ( variable ) :} {: $ variable :} {: $variable :}
The four forms have the same meaning.
Parentheses are not required provided there are no spaces between the parameters:
{: ~ $ variable <p> HTML code </p> :} <- * unnecessary {: ~ ($ variable1 == $ variable2) <p> HTML code </p> :} <- * required
Use quotation marks and commas in the parameters:
{: include ('file.nhtml','The \'Title\'','one, two and three') :}
All Tags except NES may occupy several lines:
{: # If variable returns true you see "HTML code" :} {: ~ ( $ variable ) <p>HTML code</p> :}
{: # comment :}
Block is removed and not displayed in the HTML output.
{: $ variable :}
The block is replaced by the value of "variable" that must have been previously defined in our Perl script, of not being the block is replaced by an empty string.
{: * environment_variable :}
The block is replaced by the value of environment_variable which is an environment variable Nes.
{: ~ (expression) HTML :}
The block is replaced by "HTML" if expression is true. Expression can be any valid Perl expression, including Perl regular expressions, the variable can only be a $ variable, environment *, or a literal:
{: ~ ( $ variable =~ /hello/ ) <p>variable greets us</p> :} {: ~ ( * variable ) <p>variable is defined</p> :} {: ~ ( ! * variable ) <p>variable is not defined</p> :}
The expressions that result in the assignment of a variable, return true or false but the assignment has no effect:
{: ~ ( $ variable = 1 ) <p>value does not change</p> :}
{: @ (table) HTML :}
The block is replaced and is repeated by "HTML" as many times as records have Table. We refer to each field in each record with:
{: @$ table.field :}
Table must be defined in our Perl script as an array of hashes reference.
Sample:
<table border="1"> {: @ user <tr> <td> {: @$ user.name :} </td> <td> {: @$ user.email :} </td> </tr> :} </table>
{: @$ table.field :}
Where "field" is the name of the field in the table. {: @$ table.field :} is a property of {: @ (table) HTML :} or {: sql (SQL SELECT) HTML :}
{: sql (SQL SELECT) HTML :}
The block is replaced and is repeated by "HTML" for each record returned by the query. The operation is similar to Tag @. The SQL statement must begin with SELECT.
It reads the configuration (.nes.cfg) the following variables to access the database:
DB_base = base_name DB_user = user DB_pass = password DB_driver = mysql DB_host = localhost DB_port = 3306
Sample:
<table border="1"> {: sql ( 'SELECT * FROM `table` WHERE 1 LIMIT 5;' ) <tr> <td> {: @$ table.name :} </td> <td> {: @$ table.email :} </td> </tr> :} </table>
{: include ('file') :}
The block is replaced by the file "file", file can be a nhtml, nsql, html, php, sh, text, perl, etc..
{: NES ver ('file') :}
It is similar to a line of files unix shell (#!/usr/bin/...) indicating that script must be run.
In the absence of this line is treated as HTML file (type text/html) without replacing any of Tags. If you do not need any Perl script but you want the file to be treated as replacement Tag Nes:
{: NES 1.0 ('') :}
Or explicitly stating 'none' to avoid any suggestion that this is an error or oversight:
{: NES 1.0 ('none') :}
For clarity we recommend this:
{: NES 1.0 none :}
{: & tag (param...) :}
Reserved for use by plugins.
Nes Objects are bits of code, NES, HTML, JavaScript, Perl... or a mixture. Any script can become an object Nes, so it is not necessary to rewrite the code.
We call an object with include:
{: include ( 'object.nhtml', [ parameter list ] ) :}
The objects may reside in any directory. A simple example is the object location.nhtml that redirects to another page:
{: include ( '{: * cfg_obj_top_dir :}/location.nhtml', 'http://example.com/page.nhtml', '301 Moved Permanently' ) :}
Nes Objects offers many opportunities for code reuse. Nes The idea is to have a huge library of objects, so you only have to write code for individual cases, disposing of objects commonly used for the login, sessions, etc.. work to create a Web with Nes will virtually mount templates and Web design.
With Nes can not create objects, you can invoke/include objects in your templates, but the object must be created in Perl, HTML, JavaScript, etc.. Or a mixture of all with a template Nes or not.
As an example we have encapsulated part of LWP in an Nes Object:
{: include ('{: * cfg_obj_top_dir :}/lwp.nhtml', URL, extrac, {: # default: content | status | Content-Type | title | head | body | star:-:end | :-:star:-:end:-: :} method, {: # default: GET | POST :} query, {: # query: 'name=Jose&email=jose@sample.com' :} charset, {: # default: no change | ISO | UTF-8 :} useragent {: # default: Nes/0.8 :} email {: # user agent email. default: $ENV{'SERVER_ADMIN'} :} ) :} extrac: <tag>:-:</tag> Return tag content without tags: '<tag></tag>' :-:<tag>:-:</tag>:-: Return tag content and include tags: '<tag></tag>' Defined: {: $ status :} # Status response {: $ request :} # request send {: $ Content-Type :} # Content type {: $ content :} # All HTML content {: $ title :} # Title Tag content {: $ head :} # Head Tag content {: $ body :} # Body Tag content {: $ extrac :} # extrac return value
To test our object we have created a page test_page.html of which we can extract the "Title" as follows:
{: include ('{: * cfg_obj_top_dir :}/lwp.nhtml', 'http://nes.sourceforge.net/miniblog/es/test_page.html', title ) :}
Out:
Test Page - Sample to use Nes;
We created our object so we can directly call the Perl script without the template nhtml:
{: include ('{: * cfg_obj_top_dir :}/lwp.pl', 'http://nes.sourceforge.net/miniblog/es/test_page.html', title ) :}
Out:
Test Page - Sample to use Nes;
We may also create a new template for this Perl script. lwp.pl defines the following Nes Tags:
{: $ status :} # Status response {: $ request :} # request send {: $ Content-Type :} # Content type {: $ content :} # All HTML content {: $ title :} # Title Tag content {: $ head :} # Head Tag content {: $ body :} # Body Tag content {: $ extrac :} # extrac return value
Then to create a small one-page report:
{: NES 1.0 ('./lwp.pl') :} Status:<br> {: $ status :}<br> Title:<br> {: $ title :}<br> Content type:<br> {: $ Content-Type :}<br> Head:<br> {: $ head :}<br> Extrac:<br> {: $ extrac,yes_html :}
Name as lwp_custom.nhtml, store it in the obj directory, and finally we can invoke like this:
{: include ('{: * cfg_obj_top_dir :}/lwp_custom.nhtml', 'http://nes.sourceforge.net/miniblog/es/test_page.html' 'title' ) :}
Out:
Status: 200 OK Title: Test Page - Sample to use Nes; Content type: text/html Head: <meta http-equiv="content-type" content="text/html;charset=ISO-8859-1" /> <title>Test Page - Sample to use Nes;</title> Extrac: Test Page - Sample to use Nes;