如何从php中隔离样式(css + html)

我如何隔离样式( css + html )来自 php ,就像放入php文件中的一些行包括整个样式或主题。</ p>

ex:</ p>

  echo 
eval_template(“header”) 。
eval_template(“body”)。
eval_template(“footer”)
</ code> </ pre>

所以将来我可以改变整个风格而不用触摸php文件\ n 任何想法?</ strong> </ p>
</ div>

展开原文

原文

how can i isolate the style (+) from , like put in the php file just some lines including by it the whole or theme.

ex:

echo 
    eval_template("header") .
    eval_template("body") .
    eval_template("footer")

So in future i can change the whole style without touch the php files any idea ?

dongyi2159
dongyi2159 我用ob_start或者需要..
大约 8 年之前 回复
dsljpwi494719
dsljpwi494719 令人敬畏的问题:因为,为什么没有更多的人想到这种分离!..无论如何,搜索“PHP模板”或类似;然后查看是否/如何应用任何现有解决方案。这将允许构建更好的(更有针对性的问题)。在任何情况下,HTML都相对难以完全分离,因为它依赖于结构(分离的范围因方法/模式而异:模板与逻辑?仅绑定?MVP?适配器?),但CSS相对容易分离只是普通的外部样式表。
大约 8 年之前 回复

3个回答



有很多方法可以做到这一点...... </ p>

这是一个关于模板的教程 用普通的PHP
http://www.phpro.org/tutorials/ PHP-templating.html简介 </ p>

您还可以查看那里的许多模板引擎。
tww是其中之一: http://twig.sensiolabs.org/ </ p>
</ div>

展开原文

原文

there are many ways how you could do this...

Here's a tutorial on templating in plain PHP http://www.phpro.org/tutorials/Introduction-to-PHP-templating.html

You can also take a look at the many template engines out there. twig is one of them: http://twig.sensiolabs.org/

Personally, I enjoy to do it manually.

PHP should not return both css and html code, or even better, it shoukd not return client-side code at all but rather dependencies to specific parts. You never want to modify a file that contains a lot of different things.

To separate css+html from the php code, what I usually do is a hierarchy done with include("..."); and include_once("..."). For example : include_once("header.php") > include("menu.php") > html semantic with css classes correctly initialized according to current context.

Then you import your css / js external scripts in header.php so that you never have to modify the whole thing unless everything changes or if you have a complete feature to add to the website. Same is possible for every sections of the website.

Hope this helps, for me it is incredibly reducing debug-time since everything important has to be done only once, then at the top of it, you can seperate as you wish.



您可以使用很多模板引擎来实现这一点,我更喜欢使用与symfony2框架集成的twig。</ p>

Twig非常棒,因为它非常易于使用且非常灵活,您可以使用继承创建一个公共布局,可以使用特殊标签在某些部分进行扩展和覆盖。 这是我在Symfony网站上找到的指南,但对于理解twig背后的逻辑非常有用: http://symfony.com/doc/current/book/templating.html </ p>
</ div>

展开原文

原文

There are a lot of template engines you can use to do that, i prefer use twig, that is integrated with symfony2 framework.

Twig is wonderful because is very easy to use and very flexible, you can use inheritance to create a common layout which can be extended and overwriten in some some part using special tags. This is a guide i've find on Symfony website but is very usefull to understand the logic behind twig: http://symfony.com/doc/current/book/templating.html

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐