Welcome Guest, Not a member yet? Register   Sign In
Smarty like templates?
#1

[eluser]feri_soft[/eluser]
How can i include one view in another much like smarty i dont want the smarty like syntax but the normal CI php based templates. I just need to have one view inside another so that i dont need to change all view for a menu item for example. Thanks!
#2

[eluser]sophistry[/eluser]
FAQ
#3

[eluser]kucerar[/eluser]
Maybe you want sort of flexible view code inside a static template. Here's what I do:

/app/templates/mytheme/main.html:
Code:
<html>
<head>

<link href="/app/css/main.css" rel="stylesheet" type="text/css" />

<title>Welcome to My Application Suite</title>


</head>
<body>


<!--SNIP-->

</body>
</html>

At the top and bottom of my code view:
Code:
<?php

$the_template = file_get_contents( "templates/$this->current_template/main.html" );
$snip_location = strpos($the_template,'<!--SNIP-->'); //do this calculation only once
echo substr($the_template, 0, $snip_location);

?>

<?PHP

  //flexible view stuff in the content area

?>

<?php

echo substr($the_template, $snip_location);

?>

That's it. This maybe simple but unlike Smarty internals you can read it, and already you've got sort of theme support.

-R
#4

[eluser]sophistry[/eluser]
@kucerar... this solution is similar to the ruby on rails 'yield' functionality which someone has implemented in a standard CI way using a CI hook. using hooks, you avoid putting a bunch of functional PHP in a view file. check out the link in the FAQ to find the thread or just search on yield in the CI forums here.

cheers.
#5

[eluser]kucerar[/eluser]
Thanks sofistry. I like CI hooks. Not sure if the "yield" concept is a bit confusing though. Is a string replacement?

-R
#6

[eluser]sophistry[/eluser]
agreed yield can be confusing to grasp at first - it is basically string replacement like you outlined above, but the PHP internals are not embedded in the view file.

you can put PHP in a view, but the typical way of deciding how to structure code in there is to imagine a 'designer' working with your view files. now, you may or may not be working with designers but in any case it is a good way to help decide where to put functional code versus display code.

cheers.
#7

[eluser]kucerar[/eluser]
Agreed, thanks again...think I'll be going with Layout Library though

http://codeigniter.com/wiki/layout_library/

less mystery...

-R
#8

[eluser]sophistry[/eluser]
yep. the layout library at the wiki is another variation on the string replacement idea to get a nested view. That's listed in the FAQ too, please add other solutions to the FAQ if you find something good. I've tried to collect all of them because each developer approaches the problem in a different way. Personally, I prefer coolfactor's view library.
#9

[eluser]feri_soft[/eluser]
Thanks all i found very helpful thing in your posts.




Theme © iAndrew 2016 - Forum software by © MyBB