Template lite tags - php

I really searched but found nothing.
I'm new at template lite. I add my project template_lite library and I have two files.
test.php is:
require("../src/class.template.php");
$tpl = new Template_Lite;
$tpl->assign("foo","bar");
and test.html is
<html>
<head>
<title>Document Title</title>
</head>
<body>
{$foo}
</body>
</html>
what the wrong is output:"{$foo}"

According to the documentation you need to set two variables:
There are two variables that you need to set after loading Template
Lite: $template_dir and $compile_dir.
require('/path/to/class.template.php');
$tpl = new Template_Lite;
$tpl->compile_dir = "compiled/";
$tpl->template_dir = "templates/";

Related

How can i write a Template of HTML and create that HTML file with custom usernames as files_names when the API endpoints are hit in PHP?

So basically i need that when user registers in my Android App i will make a POST request with user Name and company Name that they wanted and i will create a html page with that data in the HTML Template. and webpage will be visible like this -> www.main-website.com/username [main-website.com is the domain that i have bought]
So what I did , is that I created a variable that stores the HTML Template with custom changes by adding php variables in-between.And then created a file.html. Check this code {and also If you know a better way to do this thing pls suggest [main idea -> userAuthenticate -> webpage created with username and company name as tags (this is protoype) ]
<!DOCTYPE HTML>
<?php
if(isset($_POST['userName']) && isset($_POST['companyName'])){
$user_name = $_POST['userName'];
$company_name = $_POST['companyName'];
}
$my_var = <<<EOD
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>template</title>
</head>
<body>
<h1>This is $user_name </h1>
<h2>My company name is $company_name </h2>
</body>
</html>
EOD;
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/myText.php","wb");
fwrite($fp,$my_var);
fclose($fp);
?>

How to add code to <head></head> in HTML with PHP and then display it?

I want to create a file (index.php) that will dynamically add the below code to the head of another file called (index2.php) and then display the page with the modified code.
<link rel="stylesheet" type="text/css" href="new.css">
<script src="new.js"></script>
<script>
new();
</script>
I know the way to do this without modifying the code is
include 'index2.php';
However I do not know how to add the code to the head section of index2.php
How would I go about doing this?
Maybe it's just:
index2.php:
...
<head>
<?php require("./index.php"); ?>
</head>
...
?
Put index2.php file's head tag code in a diffrent file. and then include that php file in index.php file's tag
like
index.php
include_once('siffrent_file_name.php');
....
After doing some pondering I felt like I didn't want to parse the entire HTML document, so I added a "key" to index2.php and used this code:
$file = "index2.php";
$key = "<!-- KEY -->";
$appcode = 'link rel="stylesheet" type="text/css" href="new.css">
<script src="new.js"></script>
<script>
new();
</script';
$index = fopen($file, "r") or die("Unable to open index symlink");
$code= fread($index,filesize("index2.php"));
fclose($index);
$index_app = (preg_replace ($key, $appcode, $code));
echo($index_app);

Get content from distant URL and test

I have to get data from a .html file, into a distant server. With file_gets_content I can retrieve the informations but when I want to test it I have some problems.
For example I can have 0 or 1 into my .html page. In my .php I want to do something if the file_gets_content return 0 or 1 but for now I didn't find how I can do it
Here is my .html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
0
</body>
</html>
My PHP code :
$home = file_get_contents('http://192.168.1.XXX/wordpress/read.html');
You can use this php library https://github.com/sunra/php-simple-html-dom-parser
$dom =HtmlDomParser::file_get_html('http://192.168.1.XXX/wordpress/read.html');
$bodyText=$dom->find("body",0)->innertext;
alternative solution is
$home = file_get_contents('http://192.168.1.XXX/wordpress/read.html');
$dom = new DOMDocument();
$dom->loadHTML($home);
if(($body=$dom->getElementsByTagName("body"))->length>0){
$text=$body[0]->nodeValue
}

Forwarding tags like {blabla} to php functions

I saw this in most of CMS and forum templates. How can I make HTML tags like {blabla} and how can I forward them to PHP functions?
These are called templating systems and the style of these "tags" depends on the templating system you're using.
A basic example in PHP would be something like this:
page.tpl:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Basic templating system</title>
</head>
<body>
<h2>Welcome to our website, {{name}} !</h2>
<p>Please confirm your account. We've sent an email to: {{email}}</p>
</body>
</html>
index.php:
<?php
// Get the template's content
$template = file_get_contents("page.tpl");
// The data needed in the template
$data = array(
'name' => 'John',
'email' => 'john#smith.com',
);
// The template's tags pattern
$pattern = '{{%s}}';
// Preparing the $map array used to replace the template's tags with data values
$map = array();
foreach($data as $var => $value)
{
$map[sprintf($pattern, $var)] = $value;
}
// Replace the tags with data values
$output = strtr($template, $map);
// Output the template with replaced tags
echo $output;
?>
I recommend that you check out already existing templating engines such as: Mustache, Smarty or Twig and many others
Hope this helps :) !

CI template - element is displayed in wrong place

I am having problems displaying views with a template.
My template looks like this:
<?php
$this->load->view('includes/header');
if($this->session->userdata('is_loggedin')!=1) //check if logged in
{
$this->load->view('includes/not_loggedin'); //includes this when not logged in
}
else //includes all this when is logged in
{
if(isset($content)) //check if content variable isnt empty
{
$this->load->view($content); //THIS IS MY CONTENT WHIC IS DISPLAYED IN WRONG POS
}
$this->load->view('includes/is_loggedin'); //
}
$this->load->view('includes/footer');
?>
By wrong position, I mean that my form is being displayed in the top lefthand corner outside of the HTML structure. Here is a copy from inspect element window. Notice where the div is located; the head tags are empty; and, the head information is in body.
<html lang="en">
<head></head> //head tags are empty
<body>
<div id="settings">...</div> //this is my loaded div
<meta charset="utf-8">
<title>Sludinājumu lapa</title> //head info outside tags
<link href="/assets/css/style.css" type="text/css" rel="stylesheet">
<div id="wrapper">....</div> //i need settings div inside wrapper div
</body>
</html>
Without loading that view, the HTML structure is fine. Also there is no problems with loading is_loggedin and not_logged into the views.
My header contains :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sludinājumu lapa</title>
<link rel="stylesheet" type="text/css" href="/assets/css/style.css">
</head>
<body>
<div id="wrapper">
And the footer contains:
<foter>
<p>developed by kriss</p>
</foter>
</div> //End of "wrapper" div
</body>
</html>
From the controller I am passing the data like this:
$data['content'] = $this->load->view('vchangeinfo');
$this->load->view('template', $data);
Any ideas why everything is so messed up?
Two ways of doing this:
Codeigniter views
Load it in advance (like you're doing) and pass to the other view
There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned:
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['content'] = $this->load->view('vchangeinfo', NULL, TRUE);
$this->load->view ('template', $data);
// View
if(isset($content)) //check if content variable isnt empty
{
$this->load->view($content);
// OR don't know wich one works.
// echo $content;
}
Load a view "from within" a view:
<?php
// Controller
$this->load->view('template');
<?php
// Views : /application/views/template.php
$this->view('vchangeinfo');

Categories