allow php templates,PHP templates - with PHP [closed]

What's the most elegant templating (preferably in pure PHP!) solution you've seen?

Specifically i'm interested in handling:

Detecting in a repeating block whether it's the first or last element

Easy handling of odd/even cases, like a zebra striped table, or similar

Other modulos logic, where you'd do something every n'th time.

I'm looking for something that makes this less of a pain:

$persons = array('John', 'Jack', 'Jill', 'Jason');

?>

  • = $name ?>

Does it really take the mess above to create something like this below?

  • John
  • Jack
  • Jill
  • Jason

Is it only me that find the above near hideous?

All those starting and closing of php-tags makes me cringe.

You don't need to open the tags more than once. You can also make a function out of it if you do the same thing multiple times:

function makeul($items, $classes) {

$c = count($classes);

$out = "";

if (isset($items) && count($items) > 0) {

$out = "

  • \n";

foreach ($items as $item) {

$out .= "\t

$item\n";

}

$out .= "

\n";

}

return $out;

}

?>

other page content

$persons = array('John', 'Jack', 'Jill', 'Jason');

$classes = array('odd', 'even');

print makeul($persons, $classes);

?>

Also, if you don't mind using Javascript, Jquery makes things done mod 2 pretty easy (e.g., for zebra striping a table):

$("tr:odd").addClass("odd");

$("tr:even").addClass("even");

Tiny But Strong

It doesn't make the smarty mistake of embedding another macro language in the page, but does allow you to handle every practical web display issue I've ever thrown at it. In particular the above odd/even constructs are a doddle. For something like your code selecting from a database table

In the PHP file

$TBS->MergeBlock('blk1',$sqlconnect, "SELECT name from people ");

And in the HTML file

  • [blk.name;block=ul]
  • [blk.name;block=ul]

And that's it. Notice that the HTML is completely Dreamweaver compatible. Furthermore if I wanted to make that alternate over three line styles all I'd need to do is add the extra line, maybe with different classes, so

  • [blk.name;block=ul]
  • [blk.name;block=ul]
  • [blk.name;block=ul]

A small help on the looping:

$b=false; foreach($MyList as $name) { ?>

= htmlspecialchars($name); ?>

} ?>

By saying $b=!$b, it automatically alternates between true and false. Since false prints as "", and true prints as "1", then by defining css classes row and row1, you can get your altering rows without any trouble.

consider using :first-child css to style the first one differently.

It ain't pure PHP (the templating syntax then), but it works realy nice; Smarty.

For loops you can do:

{foreach from=$var name=loop item=test}

{if $smarty.foreach.loop.first}

This is the first item{/if}{$var.name}

{if $smarty.foreach.loop.last}

This was the last item{/if}

{/foreach}

have you considered phptal?. one main benefit of it (or something similar) is that you get templates which can pass validation. most php template engines seem to ignore this.

I use PHPTAL for templating because it is written in 100% actual HTML with placeholder data, so it even works in a WYSIWYG editor for a web designer. That and it's just way easy to understand.

Here's what it would look like for me. Please forgive the markup, I'm new here and the four spaces block wasn't working right for me (the list was a list, not the markup).

PHP Code:

$tal = new PHPTAL;

$tal->setTemplate('people.htm')

->set('people', array('John', 'Jack', 'Jill', 'Jason'));

echo $tal->execute();

Template:

  • John Doe

Output:

John

Jack

Jill

Jason

Now obviously I wouldn't make a template for this little, but I could use a macro for it or build a whole page and include that variable. But you get the idea. Using PHPTAL has just about tripled my speed at templating and programming, just by the simplicity (no new syntax to learn like smarty).

How's about XSLT? The only template system that has a standards body behind it. Works the same across programming languages. Learn it once, use it everywhere!

Jon Winstanley

Symfony Components: Templating

Symfony intends on moving to a new templating system based on the lightweight PHP templating system twig.

Symfony can usually be replied upon to make very informed decisions on such matters, so this framework should be something to look into.

I've used Smarty Template Engine in the past. It's Pretty solid. And as you can probably tell from the website; it has quite the large user-base and is updated regularly.

It's in pure PHP as well.

Savant is a lightweight, pure PHP templating engine. Version 2 has a cycle plugin similar to the Smarty one mentioned earlier. I haven't been able to find a reference to the same plugin in version 3, but I'm sure you could write it fairly easily.

If is just to apply a CSS style, why don't you use the :nth-of-type(odd) selector.

For example:

li:nth-of-type(odd) {

background: #f2f6f8;

background: linear-gradient(top, #f2f6f8 0%, #e0eff9 100%);

}

I use Modulo like you did in your example all the time.

If what cringes you is the opening and closing tags, write a function that creates the html string and then have it return it. At least it will save you some tags.

I have been a fan of HAML for quite a while, it looks like PHP folk have HAML now: see http://phphaml.sourceforge.net/

= ($i++ % 2 === 0) ? 'odd' : 'even' ?>

You're doing it the other way around. Your first item is now called even instead of odd. Use ++$i.

I'm having the same problem. But I think your original solution is the neatest. So I'll go with that.

I created a simple templating system in PHP to solve this problem a while ago:

It takes a multidimensional array, and requires the addition of some extra tags to the HMTL to create the combined template.

It's not as complicated (albeit powerful) as Smarty and some other solutons, but wins out in simplicity a lot of the time. A demo of the menu creation:

Main Menu

{block:menu_items}

{var:name}

{/block:menu_items}

Merged with...

array (

'menu_items' => array (

array (

'link' => 'home.htm',

'name' => 'Home'

),

array (

'link' => 'about.htm',

'name' => 'About Us'

),

array (

'link' => 'portfolio.htm',

'name' => 'Portfolio'

),

array (

'link' => 'contact.htm',

'name' => 'Contact Us'

)

)

);

Will create the menu...

Main Menu

define ('CRLF', "\r\n");

$persons = array('John', 'Jack', 'Jill', 'Jason');

$color = 'white'; // Init $color for striped list

$ho = '

  • ' . CRLF; // Start HTML Output variable

foreach ($persons as $name)

{

$ho .= '

' . $name . '' . CRLF;

$color = ($color == 'white') ? 'grey' : 'white'; // if white, make it grey else white

}

$ho .= '

' . CRLF;

echo $ho;

?>

来源:https://stackoverflow.com/questions/163834/php-templates-with-php

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值