How to use Varnish ESI with modular PHP templating system - php

I am looking to implement varnish into a data heavy/user-centric website. How do I setup ESI using a system which uses php to include html templates?
I currently use a custom php templating system (similar to an MVC design pattern) which works like this:
make page request -> php calculates logic -> php includes html template pages and fills out variables -> page is output
I've only ever seen esi tags used in predominantly html pages, to include php snippets.
like the code below:
<HTML>
<BODY>
The time is: <esi:include src="/php-includes/date.php"/>
at this very moment.
</BODY>
</HTML>
But can it be done the other way around? e.g. esi tags in php pages to include html snippets?
Similar to this:
<?php
//logic here
$content = "this will be displayed on the content page"
include("templates/header.html.php"); //esi would go here since page is static content
include("templates/content.html.php"); //no esi here, since page is dynamic content
include("templates/footer.html.php"); //esi would go here since page is static content
?>

You just have to create a kind of "ESI" renderer thingy for your specific MVC implementation, as in /esi.php?template=foo, then inside something like:
... whatever you need to boostrap your app in order to render a template ....
include("templates/$template.html.php");
.... exit so no header/fooder stuff is rendered, only template HTML of interest
Surely not as simple as that but in a nutshell a similar thing.
I would then put some logic atop each template file to emit either the HTML (if rendered by esi.php or if ESI "feature" is in disabled state) or <esi tag only.
So for example templates/header.html.php could have (pseudo-code):
if esi (detect by checking request_uri to be esi.php) then echo '<esi /esi.php?template=header?somevar-from-parent=...'; return
---existing code--

Related

How to retrieve content in layout zend framework2?

I was using phtml files in zend framework. Now I am using .tpl files.
I found how to use html script and all. But when I want to use php code. Then I'm using:
<?php
echo "test";
echo $this->content;
?>
The problem with this is it is in layout.tpl file. Main content is in index.tpl of other module.
Rather than fetching the content of index file It echoing just 'test'.How to make it works?
Edited: I also tried {$this->content}.
If you are using the Smarty Templating Engine and the SmartyModule, then you will have to use Smarty syntax in your view scripts, since the Zend\View\Renderer\PhpRenderer will be overridden by the Smarty Renderer (and the Smarty Templating Engine). Also, if you wish to use layouts with Smarty, please see Smarty's Template Inheritance mechanism. Here is an example:
layout.tpl
<html>
<head>
<title>{block name=title}Default Page Title{/block}</title>
</head>
<body>
{block name=body}{/block}
</body>
</html>
mypage.tpl
{extends file="layout.tpl"}
{block name=title}My Page Title{/block}
{block name=body}My HTML Page Body goes here{/block}
Otherwise, if you are using the PhpRenderer, it will not "recognize" any templating language, even if you change the view script's file extension to .tpl, since it will simply include the content of the view scripts (see lines 502-503 of the renderer's source code). Therefore, as with any include, the PHP code will be executed immediately and stored in the renderer's $__content property. This is probably the reason why your echo command is immediately executed.
So, basically, you will have to choose your renderer (PhpRenderer or Smarty Renderer through the SmartyModule) and then abide by its inner workings (PHP/HTML or Smarty syntax (ex. variables), respectively).

Minimize code size to avoid duplication of the same code

I need one advice from you. I am working on a website, which uses PHP and HTML. As the biggest part of the header and footer code will be same for many pages, I am thinking of using PHP's include to avoid code duplication. But, each of those pages requires different stylesheets and JS files included. What do you think how could I let the other file know what scripts and stylesheet to import?
Our company does this:
The header reads the filename of the page calling it when it's included.
Then, it changes the extension to '.js' and outputs that if it exists. Same for CSS.
So if I have a page "register.php", it will auto-include "register.js" and "register.css" if they exist.
Here's what I do:
<?php include("includes/headContent.php"); ?>
<title>Page title goes here!</title>
<script src="script_only_used_on_this_page"></script>
<?php
require_once("includes/siteHeader.php");
?>
Site Content Goes Here!!
<?php
require_once("includes/siteFooter.php");
?>
Head Content includes any PHP I want included in every page, as well as the opening html and head tag, and any Javascript libraries and css stylesheets I want on every page. Site header closes the /head tag, and opens the body as well as printing out my site header and some other markup that goes on every page. Finally Site Footer closes out my template. Everything in between is my content area!
There are lots of different ways you can do templating, if you wanted to create a simple include and an echoHeader() and an echoFooter() function... just have the echoHeader function accept a parameter which you would pass your javascript and CSS lines to.
you can use MVC coding pattern

How can I fetch the HTML contents of one single TYPO3 page into another CMS?

We need to display the content of one single TYPO3 page in Habari.
It would suffice to retrieve the HTML, as styling (CSS) is done separatly.
However, we only want the HTML of the content elements - not the whole, fully rendered page.
How could we achieve that?
Does TYPO3 (or one of its plugins) provide a facility for that?
This can be done via a custom Typoscript template-record in the Typo3 backend that just outputs the content without any further HTML and or tags.
Putting something like this in the 'setup':
page = PAGE
page.config.disableAllHeaderCode = 1
page.10 < styles.content.get
Then make sure in the template-record it say's that it's a root-template, and that it clears constants and setup before this template. And put this record on the top most page (aka root).
Also make sure that you included the static template of CSS Styled Content. This can be done when editing the template-record inside Typo3.
You could do this in Habari using something like this:
$url = "http://your-typo3-url/";
$output = RemoteRequest::get_contents( $url );
$output will then be the HTML contents of the page. You can then use a combination of strpos() and substr() to pull the relevant HTML content you want, eg just the <body>
You can do this in one of your theme template files, the theme's theme.php file itself or even within a plugin.
You can then use Habari's native caching to cache the content too so you don't have to retrieve the Typo3 page with every page view.
BTW, You could use typo3_webservice fro that. It uses XMLRPC protocol, and quite easy to implement with PHP.
http://typo3.org/extensions/repository/view/typo3_webservice/current/

asp.net include page like php

im starting to learn asp.net :P, and in php i should use include(); to show something like a menu. but how can i do this in asp.net?
i dont mean a simple include, but a page where i can query a database to make the page dynamicly for example.
i hope you understand my problem.
You do this by Developing a Simple ASP.NET Server Control. Before that read ASP.NET Server Control Development Basics
And then it will be just as easy as doing
<%# Register TagPrefix="stefanControls" Namespace="stefanNamespace" Assembly = "stefanAssembly" %>
<html>
<body>
<form runat=server>
Here is a custom ASP.NET server control.<br><br>
<stefanControls:MenuBar Depth="2" runat=server/>
<br>
</form>
</body>
</html>
with your page template.
Rather than trying to write ASP.NET code like PHP (and using include files), I would recommend creating a user control (.ascx) for the content that you want to dynamically render, and dropping that onto your page. You can write the code to retrieve dynamic content for the control in the code-behind.
More info:
http://msdn.microsoft.com/en-us/library/y6wb1a0e.aspx
Use Custom Server Controls, or Master Pages. Both are a bit unusual at first if coming from PHP/Classic ASP, but they are great and make your life a lot easier.
Well,you could create a Class (.cs) for these purposes and create a function such as
class MyClass
{
public static string GenerateHTML()
{
string html="";
html = "<Your HTML>";
return html;
}
}
and just call MyClass.GenerateHTML() wherever you want to have that HTML placed.....This method is much similar to include in PHP........for including HTML content and obviously this method is good for defining all common functions as well...

How is duplicate HTML represented in your codebase, in a non-duplicate way?

Most HTML in a large website is duplicated across pages (the header, footer, navigation menus, etc.). How do you design your code so that all this duplicate HTML is not actually duplicated in your code? For example, if I want to change my navigation links from a <ul> to a <ol>, I'd like to make that change in just one file.
Here's how I've seen one particular codebase handle this problem. The code for every page looks like this:
print_top_html();
/* all the code/HTML for this particular page */
print_bottom_html();
But I feel uncomfortable with this approach (partially because opening tags aren't in the same file as their closing tags).
Is there a better way?
I mostly work with PHP sites, but I'd be interested in hearing solutions for other languages (I'm not sure if this question is language-agnostic).
I'm not a php programmer, but I know we can use a templating system called Smarty that it works with templates(views), something like asp.net mvc does with Razor.
look here http://www.smarty.net/
One solution at least in the case of PHP (and other programming languages) is templates. Instead of having two functions like you have above it would instead be a mix of HTML and PHP like this.
<html>
<head>
<title><?php print $page_title ?></title>
<?php print $styles ?>
<?php print $scripts ?>
</head>
<body>
<div id="nav">
<?php print $nav ?>
</div>
<div id="content">
<?php print $content ?>
</div>
</body>
</html>
Each variable within this template would contain HTML that was produced by another template, HTML produced by a function, or also content from a database. There are a number of PHP template engines which operate in more or less this manner.
You create a template for HTML that you would generally use over and over again. Then to use it would be something like this.
<?php
$vars['nav'] = _generate_nav();
$vars['content'] = "This is the page content."
extract($vars); // Extracts variables from an array, see php.net docs
include 'page_template.php'; // Or whatever you want to name your template
It's a pretty flexible way of doing things and one which a lot of frameworks and content management systems use.
Here's a really, really simplified version of a common method.
layout.php
<html>
<body>
<?php echo $content; ?>
</body>
</html>
Then
whatever_page.php
<?php
$content = "Hello World";
include( 'layout.php' );
Sounds like you need to use include() or require()
<?php
include("header.inc.php");
output html code for page
include("footer.inc.php");
?>
The header and footer files can hold all the common HTML for the site.
You asked for how other languages handle this, and I didn't see anything other than PHP, so I encourage you to check out Rails. Rails convention is elegant, and reflects #codeincarnate 's version in PHP.
In the MVC framework, the current view is rendered inside of a controller-specific layout file that encapsulates the current method's corresponding view. It uses a "yield" method to identify a section where view content should be inserted. A common layout file looks like this:
<html>
<head>
<% #stylesheet and js includes %>
<body>
<div id="header">Header content, menus, etc…</div>
<%= yield %>
<div id="footer">Footer content</div>
</body>
</html>
This enables the application to have a different look and feel or different navigation based on the controller. In practice, I haven't used different layout files for each controller, but instead rely on the default layout, which is named "application".
However, let's say you had a company website, with separate controllers for "information", "blog", and "admin". You could then change the navigation for each in a clean and unobtrusive manner by handling the different layout views in their respective layout files that correspond to their controllers.
You can always set a custom layout in the controller method by stating:
render :layout => 'custom_layout'
There are also great helper methods built into Rails so you don't have to rely on $global variables in PHP to ensure your CSS and Javascript paths are correct depending on your development environment (dev, staging, prod…). The most common are:
#looks in public/stylesheets and assumes it's a css file
stylesheet_link_tag "filename_without_extension"
#looks in public/javascripts and assumes it's a js file
javascript_include_tag "jquery"
Of course, each of these sections could be expounded upon in much greater detail and this is just brushing the surface. Check out the following for more detail:
http://guides.rubyonrails.org/layouts_and_rendering.html
What you suggested works OK. As long as print_top_html and print_bottom_html stay in sync (and you can use automated tests to check this), then you never need to worry about them again, leaving you to focus on the real content of the site -- the stuff in the middle.
Alternatively, you can combine print_top_html and print_bottom_html into a single call, and send it HTML code (or a callback) to place in the middle.
I use the partials system of Zend_View (very similar to Rails). A partial is essentially a small HTML template that has its own variable scope. It can be called from inside views like:
<?php echo $this->partial('my_partial.phtml', array( 'var1' => $myvar ));
The variables that get passed into the construct get bound to local variables inside the partial itself. Very handy for re-use.
You can also render a partial from inside normal code, if you're writing a helper object where you have more complex logic than you'd normally feel comfortable putting in a view.
public function helperFunction()
{
// complex logic here
$html = $this->getView()->partial('my_partial.phtml', array('var1' => $myvar ));
return $html;
}
Then in your view
<?php echo $this->myHelper()->helperFunction(); ?>

Categories