Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can we use Template engine for Get-Simple need suggestion from GS Team
#26
Since I had to put a delay in my GS development... here is the code for the Template for the curious ones (it's slightly more than 1 KB :-) :

PHP Code:
<?php

class Template {
    var 
$vars;

    function 
Template() {
    }

    public static function 
factory() {
        return new 
Template();
    }

    
/**
    * Clear the templates variables
    */
    
function clear() {
        unset(
$this->vars);
        return 
$this;
    }

    
/**
    * Set a template variable.
    */
    
function set($name$value) {
        
$this->vars[$name] = $value;
        return 
$this;
    }

    
/**
    * Open, parse, and return the template file.
    *
    * @param $_p_file string the template file name
    * (name obfuscated to avoid clashes with template variables names)
    */
    
function fetch($_p_file) {
        if (!empty(
$this->vars)) {
            
extract($this->vars); // Extract the vars to local namespace
        
}
        
ob_start(); // Start output buffering
        
if (is_readable($_p_file)) {
            include(
$_p_file); // Include the file
        
} else {
            
debug('could not find file'$_p_file);
        }
        
$contents ob_get_contents(); // Get the contents of the buffer
        
ob_end_clean(); // End buffering and discard
        
return $contents// Return the contents
    
}
// Template() 

A template looks like this:

PHP Code:
<html>
<
head>
<
tittle><?= $title ?></title>
</head>
<body>
<h1><?= $title ?></h1>
<?= $content ?>
</body>
</html> 

And can be called in the following way:

PHP Code:
$content '<p>...</p>';
Template::factory()->
  
set('title''my fancy title')->
  
set('content'$content)->
  
fetch('filename.php'); 

You can put any php code into the template but you should really really try to avoid anything fancier than ifs, foreachs and echoes.

ciao
a.l.e

p.s.: it's trivial code, so consider it public domain...
p.p.s.: i have versions of it that optionally read from a string instead of a file...
p.p.p.s.: i have not tested the sample code above
Reply
#27
Hello ALE,

Quote:p.p.s.: i have versions of it that optionally read from a string instead of a file...
This would be interesting.
Could you share the advanced version?

Kind Regards,
Serenado
Reply
#28
hi serenado,

(2013-02-25, 01:09:26)Serenado Wrote:
Quote:p.p.s.: i have versions of it that optionally read from a string instead of a file...
This would be interesting.
Could you share the advanced version?

i'm a bit hesitant to post the snippet in here.

i fear that people could (mis)use it, to output unfiltered php code coming from untrusted users.

it's nothing i've invented my self and there are several resources online that explain multiple ways to achieve this. (btw, none of them is safe...)
if you're proficient enough in php to understand in which cases it's ok to use each of them, i'm sure that you will be able to google for them :-)

i have fewer problems in putting the code in a library, since the effort requested to look for the library, understand the code surrounding the snippet and learn how to use it in your code, is an indicator that you (mostly) know what you're doing.

ciao
a.l.e
Reply
#29
Hello ale,

thank you for your reply. I understand your concern.

Actually I can work with both concepts - a small Template Engine like RainTPL and something completely PHP based aswell.
Both have its advantages and shortcomings.

I think I'll create a "fork" of the GS Blog with the use of the small RainTPL Engine and upload it for demonstration.
I think people are afraid things will be too complicated and because they have no examples they can't truly make their mind up to something new.
In fact things can become much easier by the use of a Template Engine.

Kind regards,
Serenado
Reply
#30
(2013-03-02, 00:58:50)Serenado Wrote: I think I'll create a "fork" of the GS Blog with the use of the small RainTPL Engine and upload it for demonstration.
I think people are afraid things will be too complicated and because they have no examples they can't truly make their mind up to something new.

well, using a template engine, makes indeed the code a bit more complex, because the author has to take care to separate the logic from the presentation and can't simply output the data as soon it's ready.

so, simple, straightforward scripts are not possible anymore.

but, at the same time, it forces the programmer to think a bit more about the structure of her code. and it's very likely that the code becomes much easier to read and maintain.

still, i tend to prefer those tiny template engines that allow to use php itself in the template :-)

good luck with the blog plugin!
a.l.e
Reply
#31
The whole point of a a templating system is that non coders can write a block or modules without doing anything special.

There are even template engines that use pure HTML.
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply
#32
hi shawn,

the problem with the template engines that propose an own syntax: they tend to add more and more functionality until they somehow match what php can do. but, by trying to create a simpler syntax, they tend to create a more complex and uglier one.

in my eyes, the main goal of templating is rather to help out in separating logic and presentation.

... all in all: for non trivial cases (loops, if, sanitizing), i've not seen a template language which is easier to understand (even for designer) than php. YMMV.

... if used correctly, i think that php is simple enough for a designer to understand the parts that she needs.

the big problem with using php as a template language is rather that having the power of php at your fingers, you may be tempted to put more and more logic into the template. a template engine -- by being more limited in its scope -- would put a natural limit it (i'm supposing here that the author of the template engine, will refuse to implement the changes that would make it as powerful as php... and those requests will come! one after the other!).
Reply
#33
Hello ale,
(2013-03-02, 08:46:54)ale Wrote: well, using a template engine, makes indeed the code a bit more complex, because the author has to take care to separate the logic from the presentation and can't simply output the data as soon it's ready.

so, simple, straightforward scripts are not possible anymore.
I think a balance between the designers simplicity and coders diligence if fair enough.

(2013-03-02, 08:46:54)ale Wrote: but, at the same time, it forces the programmer to think a bit more about the structure of her code. and it's very likely that the code becomes much easier to read and maintain.
Exactly.

At the moment the designer needs to handle (and/or understand) all the code in the template vicinity. And after the module was updated he needs to reproduce his template settings, again in the php vicinity.
It's inconvenient for both - the coder and designer aswell.

Kind regards,
Serenado
Reply
#34
Hi,

MiniTemplater is an interesting template engine that would fit with GS

Scroll down the page to where you see the php version:

https://www.source-code.biz/MiniTemplator/

Enjoy,
F.
Reply
#35
Quote:I would like to see something basic like moustache.

Hi,
Here is a real cool template engine: slate.
It is fast, easy and powerful.

About
https://www.shayanderson.com/projects/sl...engine.htm

Quick Start
https://www.shayanderson.com/slate-php-t...-start.htm

Documentation
https://www.shayanderson.com/slate-php-t...tation.htm
Reply




Users browsing this thread: 1 Guest(s)