Our current site is broken down into various easy to work with PHP includes that are brought together using one of those nifty PHP templating libraries.
We currently use an Ant build to optimize most of our front-end code in regards to concatenating, minifying and image optimization. What we would like to do is add an additional Ant task that will parse the PHP template files and output static HTML pages into our build folder.
Could anyone point me in the right direction?
A very basic example below of what I would like to achieve:
PHP template before build
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/tpl/basic-template.php');
startblock('title');
echo 'Test page';
endblock();
startblock('content');
include($_SERVER['DOCUMENT_ROOT'].'/incl/content-fragment.php');
endblock();
?>
Is it possible through an Ant task to create the static HTML page of the above as so:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test page</title>
</head>
<body>
<p>This paragraph was the contents of content-fragment.php</p>
</body>
</html>
You could use the Ant Get task by hosting files in a build server that is running apache:
<get src="http://buildserver/index.php" dest="app/index.html"/>
Related
Hello, I started learning PHP, I set myself a goal to make a website,
which would be similar to social network, as I am newbie, I don't know what is good or bad, I can code simple functions, have fundamentals of HTML, CSS, JS. But I want to learn more deeply, like what practices should I use and which I shouldn't
Here is the code of header.php :
<?php
$page = $_SESSION['page'];
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $page ?></title>
</head>
<body>
<h3>HEADER</h3>
</body>
</html>
<hr>
And here is the code of index.php :
<?php
session_start();
$_SESSION['page'] = "Pradinis Puslapis";
require('tpl/header.php');
echo "Hello World!";
?>
<hr>
Of course, I would include footer too, and also in index.php I would implement html code specially designed for that page, but like I wrote, I want to know If I am thinking good - to send page's name through
$_SESSION variable.
You don't need a session in your case. When you include your header you can use your variables directly in your included script. When you have a template engine you take another way. In that case you load a template file and replace your variables and return the parsed HTML code to your PHP file and print them out.
main.php
$page = "Test";
require('tpl/header.php');
header.php
<!DOCTYPE html>
<html>
<head>
<title><?php echo $page ?></title>
</head>
<body>
<h3>HEADER</h3>
</body>
</html>
<hr>
What you try here is good for learning but not really common in production. There are a ton of really good Frameworks that gives you a perfect structure and help you to prevent mistakes. For your Programm you could start with a template engine like Twig or Dwoo for example. So first you should learn the basics then you can build a website on your own or good tools. Building a good framework is really hard.
i would like to do some JQuery stuffs on the content of the php buffer before sending the content of the buffer. I have this code:
<?php ob_start() ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Titre</title>
<link rel="stylesheet/ucss" href="style.css" />
</head>
<body>
//some html and php code
</body>
</html>
<?php $bufferContent = ob_get_clean(); ?>
Also I'd like to do some DOM stuff with JQuery on the content of the $bufferContent var.
I already know the V8JS PECL extension but i don't know how to use it with JQuery and DOM function.
Thanks for help and excuse my english.
Remember the basics - PHP runs on server and JS (jQuery too) runs in the user's browser. You can not run jQuery on the server side (only if you start browser there :)), but it is useless)
You can use PHPQuery to run most JQuery commands.
However to run commands that use certain elements like document / window / etc you will need not only a JavaScript engine (v8js) but a DOM to work with the engine. For that there is Env-JS and Node-JS.
You will have to load that into v8 before loading jquery-min.js.
I've been making a website with about 25 pages and every time I want to update the nav bar, or the footer, I have to go through every page and make the change!
Surely there is a way to have the header and the footer (and the side bar!) in separate documents and call them in the same way a CSS is called so they don't have to be repeated on every page.
Bonus: Is this likely to affect SEO in any way? Might it slow down the site?
Thank you,
Tara
by using include():
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Your page</title>
<style type="text/css">
<!--Your styles ect that make your page-->
</style></head>
<body>
<div class="container">
<div class="header"><?php include('./site_content/header.html');?></div>
<div class="sidebar"><?php include('./site_content/sidebar.html');?></div>
<div class="content">
your content
</div>
<div class="footer"><?php include('./site_content/footer.html');?></div>
</div>
</body>
</html>
HTML itself - ignoring framesets and iframes which do have an effect on SEO and are generally not really recommended - does not have any method to include partial HTML.
You can however use PHP (or SSI if you're oldskool) for such. It has a command to include partial files, it's called include PHP Manual.
PHP needs to be activated on your server for it. To keep this transparent you might want to map the .html file extension to PHP or use Mod_Rewrite to do that. That depends on the type of server and it's configuration.
Might it slow down the site?
The server has more work to do to process the request, therefore it slows down the site a little bit. But normally for a simple site you won't notice that much.
To prevent such, it's possible to build a simple caching mechanism on top that will convert the dynamic PHP output into static files on the fly.
i am new to .php. I would like to know what are all the ways we can create User Controls (How we do it in asp.net). This found with include in php, but i need to pass parameters to it and use those parameters in that php include file.
I would strongly encourage you to use a php framework for web development. Standalone php is too generic and unstructured to be effective in development.
To answer your question, if you decide to use a framework such as Symfony you will find it comes equipped to handle "User Controls" using "Partials", "Components", and "Widgets".
See http://www.symfony-project.org/book/1_2/07-Inside-the-View-Layer for more information.
--
If you decide not to use a framework, then your best bet would be to put the HTML code, in an include file (e.g. myControl.inc.php) and then include it manually in your main layout using:
Again, I'd strongly discourage anyone from developing a php application without a framework.
I remember I did an experiment to mimic ASP.NET behaviour using PHP + JavaScript.
A vague example:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict (jaFramework-Edition)//EN//"
"http://www.joelalejandro.com/ja-xhtml/ja-xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ja="http://www.joelalejandro.com/ja-xhtml/" xml:lang="en" lang="en">
<head>
<title></title>
<script type="text/javascript" src="jaXHTML.js"></script>
</head>
<body>
<ja:MySQLServer
id="MySQL1"
ConfigFile="mysql1.conf">
</ja:MySQLServer>
<ja:Grid
id="Grid1"
UsingConnection="MySQL1"
DataSource="urminute_com.mp_songs">
</ja:Grid>
<script>
alert(window.Grid1.DataSource);
</script>
</body>
I took XHTML 1.0 Strict's definition file and added the "ja:" tags. Then, using Javascript DOM and AJAX, I replaced the tags with the content that I was required to deliver.
I don't know exactly how efficient the solution is, yet is XHTML-compliant.
Just my two cents on the subject.
Can't this be achived using "get" parameters in the include call? Something like:
include 'control.php?param1=value1';
I'm used to working in ASP.NET / ASP.NET MVC and now for class I have to make a PHP website.
What is the equivalent to Master Views from ASP.NET in the PHP world?
Ideally I would like to be able to define a page layout with something like:
Master.php
<html>
<head>
<title>My WebSite</title>
<?php headcontent?>
</head>
<body>
<?php bodycontent?>
</body>
</html>
and then have my other PHP pages inherit from Master, so I can insert into those predefined places.
Is this possible in PHP?
Right now I have the top half of my page defined as "Header.html" and the bottom half is "footer.html" and I include_once both of them on each page I create. However, this isn't ideal for when I want to be able to insert into multiple places on my master page such as being able to insert content into the head.
Can someone skilled in PHP point me in the right direction?
You can use Smarty or develop your own template engine.
Basically you can define a simple layout like this:
layout.tpl:
<html>
<head>
<title>My WebSite</title>
{$header}
</head>
<body>
{$bodycontent}
</body>
</html>
define your header:
header.tpl
<script type="text/javascript" src="js/myjs.js"></script>
and your content file:
home.tpl
<div>hello {$var}! </div>
And finally your controller:
index.php
<?php
$header = new Smarty();
$headerOutput = $header->fetch('header.tpl');
$content = new Smarty();
$content->assign('var', 'world');
$contentOutput = $content->fetch('home.tpl');
$layout = new Smarty();
$layout->display('header', $headerOutput);
$layout->display('bodycontent', $contentOutput);
$layout->display('layout.tpl');
?>
Since PHP is a programming language and not a framework, it doesn't have that functionality out of the box.
There are many solutions to this problem, probably the most complex one would be to actually use a framework. There are many PHP frameworks to choose from. Downside would be that you'd be learning a framework and not PHP.
A simpler solution is to use a Templating engine. You'd be closer to PHP but you'd still have to learn how to use such engine.
And you're already using the simplest solution: including files from a main file. You can also include PHP files that is going to get executed and not only static html.
If you don't want to use a Templating engine, one solution could be like this:
<html>
<head>
<title>My WebSite</title>
<?php include('headcontent'); ?>
</head>
<body>
<?php include('bodycontent'); ?>
</body>
</html>
And in bodycontent:
<?php
switch ($_GET['page'];
case 1:
include('page1');
break;
case 2:
include('page2');
break;
}
?>
bodycontent will be like a very simple controller.