Are there any more Unobtrusive Server-side Scripts like hQuery for easier templating?

152 Views Asked by At

I came across this interesting templating tool, what the author calls as hQuery which is an 'Unobtrusive Server-side Scripting'. [More information here - https://github.com/choonkeat/hquery ]. It is built in Ruby for the RoR platform.

I wanted to know if something similar is available for other platforms (PHP, Python, Java)


PS : I know about templating engines like smarty and twig. I'm looking for something closer to hQuery.

2

There are 2 best solutions below

0
On

I don't like using other templating engines much, really because I find them a little to heavyweight for anything I actually want to do (smarty for example).

There is a school of thinking that would say: PHP is already a templating engine... why build templates within templates?

I do disagree with this to an extent, I find templating very useful in abstracting HTML from PHP code.

Below is an edited method from my templating class that I use that will explain how easy it is to actually make yourself.

$params = array("<!--[CONTENT]-->" => "This is some content!");
$path = "htmltemplates/index.html";

$html = implode("",file($path));

foreach($params as $field => $value) {
    $html = str_ireplace($field, $value, $html);
}

echo $html;

There is quite a bit more meat around this, but this is the core code. Read a file into an array, implode, search through the $params array and replace $field with $value in $html. Output the edited $html.

your index.html file will look something like:

<html>
<head>
<title>This is a template</title>
</head>
<body>
<div id="page-container">
    <!--[CONTENT]-->
</div>
</body>
</html>

Your output will be:

<div id="page-container">
    This is some page content!
</div>

Maybe look at implementing your own templating engine! :)

1
On

Not that I know of, but I have been doing something similar in concept, although a lot more simple, in PHP using phpQyery and some custom html-like markup.

For instance, here is a simplified non-standard html chunk:

<bodynode>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<div class="holder">
    <article>
    <header class="col_12f">
        <component id="logo"></component>
        <component id="address"></component>
        <component id="languages"></component>
        <component id="mainmenu"></component>
    </header>
    <section id="banner">
        <component id="maingallery"></component>

        <component id='sideMenu'></component>
    </section>
    <section class="col6 first" id="intro_title">
        <h1 class="underlined"></h1>
        <section class="col3 first" id="intro_col1"></section>
        <section class="col3 last" id="intro_col2"></section>
    </section>
    <section class="col3" id="location"></section>
    <section class="col3 last" id="services"></section>
    </article>
    <div class="clear"></div>
</div>
<component id="footer"></component>
</bodynode>

Using phpQuery, which works server-side with XML and HTML Dom Nodes, in a way very similar to jQuery, I map all tags with content coming from the db, using their ID as key. as well as all <component></component> tags with custom output from functions. So the existence of a <component id="logo"></component> would cause the call of a function called component_logo, using:

function replaceComponents ($pqInput){
    $pqDoc = phpQuery::newDocument($pqInput);
    $comps = pq('component');
    foreach ($comps as $comp){
        $compFunc = 'component_'.pq($comp)->attr('id');
        pq($comp)->replaceWith($compFunc($comp));
    }
    return $pqDoc;
}

and

function component_logo($comp){
    $pqComp = phpQuery::newDocument(file_get_contents('Templates/Components/logo.component.html'));
    $pqComp->find('a')->attr('href','/'.currentLanguage().'/')->attr('title','Website Title');
    $pqComp->find('img')->attr('src','/Gfx/logo.png');
    return $pqComp;
}

Although it's not based on a MVC pattern and uses straight procedural programming, so far this method has allowed for a very quick development of small to medium sized sites, while keeping things nicely DRY.