开发者

Is there a faster way to do something like this html tag creating function?

My friend and I who is a bit of a newbie like me were discussing writing a PHP function to do some shorthand html..

For example:

function hf($tag,$var) {

    return '<' . $tag . '>' . $var . '</' . $tag . '>';

}

echo hf('h1','howdy');

Of course, this 'shortcut' actually takes longer to write in the end than just:

<h1>howdy</h1>

I think its still even longer than if in php having to type:

$var = '<h1>howdy</h1>';

But maybe starts becoming a little more closer to being a shortcut when doing some开发者_StackOverflowthing like:

$var = '<div>' . hf('h1','howdy') . '</div>';

But really - its not a shortcut at all. Maybe only on larger tags with 3 or more characters does it start becoming a teeny shortcut.

I thought though we would take it to the experts out there to see if you have thought of a 'shorthand' way of producing html while working in php.

I think textmates shorthand is pretty excellent ie:

h1. howdy

That seems to trump them all but I haven't seen a textmate serverside parser or something like that out there.

Have you got a faster method for doing this? My friend and I both agree that all this open and closing and forward slashes gets a bit tedious and its a bit like finger dancing sometimes when you're working in php handling doing all the apostrophes and fullstops also.


I think the "TextMate shorthand" you refer to is probably Textile, for which there is a good PHP parser (look for "Get Textile" in the right sidebar).

A similar lightweight markup language is Markdown, a version of which is used here on Stack Overflow, and for which there are several good PHP parsers like this one.

Finally there are languages like Haml and Jade that abstract away a lot of the repetition in HTML while providing a more complete templating featureset. Haml, too, has PHP libraries available.

I usually end up writing Plain Old HTML, but when you want something a little (or a lot) tighter there are a lot of options.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜