3

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 ?

2
  • Awesome question: as in, why don't more people think about this separation! .. anyway, search for "PHP template" or similar; then see if/how any of the existing solutions apply. This will allow constructing a better (and more directed question). In any case, HTML is relatively hard to separate entirely as it is structure-dependent (the range of separation varies with approaches/patterns: template with logic? bind-only? MVP? adapters?), but CSS is relative easy to separate with just normal external stylesheets. Commented Jul 13, 2012 at 21:13
  • I do with ob_start or requires .. Commented Jul 13, 2012 at 22:02

3 Answers 3

1

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/

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.