Git Product home page Git Product logo

samson's Introduction

#Samson

Samson is a simple tiny template engine without a dedicated syntax in version 1.0.

Samson supports the following:

  • CSRF Protection
  • Automatically escaped variables
  • Forms w/ input fields, select fields, buttons, and textarea. Labels are automatically created.
  • Custom Elements
  • Partial templates
<!DOCTYPE HTML>
<html lang="en">
<head>
	<title><?php echo $pageTitle; ?></title>
</head>

<body>
	<h1><?php echo $username; ?></h1>
	<?php echo $testElement; ?>
	<p><?php echo $password; ?></p>
	</br>
	<?php echo $form; ?>
	<footer>This is a footer.</footer>
</body>
</html>

Using the methods below, it will convert those variables to their actual values.

use Samson\Samson;
use Samson\Template;
use Samson\Element;

// Instantiate the template engine with the element manager as a parameter (mandatory if using custom elements)
$samson = new Samson(new Element\ElementManager());

// Set the template directory
$samson->setTemplateDir('src/views/');

// Add the header partial template by injecting a Template object into the addPartial method
$samson->addPartial(new Template('header.tpl', ['pageTitle' => 'Welcome to Samson!']));

// Create a custom element with a variable name and ID of 'testElement'
$samson->addElement(new Element\Element('div', '<p>Here is a nice div.</p>', array('id' => 'testElement', 'class' => 'full-span')));

// Create a form with an input field, select field, textarea and button
$form = $samson->form(array('action' => '', 'method' => 'POST'))
				->input(array('type' => 'text', 'id' => 'username', 'name' => 'username'))
				->select(array('name' => 'role', 'class' => 'form-control', 'options' => array('admin' => 'Admin', 'mod' => 'Mod', 'user' => 'User')))
				->textarea(array('name' => 'description', 'content' => 'I would really like to be an admin.', 'class' => 'sumo'))
				->button(array('type' => 'submit', 'name' => 'submit' , 'class' => 'btn btn-primary', 'text' => 'Create'))
				->create();

// Add the main template by injecting another Template object into the addTemplate method. The second parameter takes the values to be output
$samson->addTemplate(new Template('user.tpl', ['username' => 'Nick Tucci', 'password' => 'password', 'form' => $form]));

// Add yet another partial template, this time the footer
$samson->addPartial(new Template('footer.tpl'));

// Finally render the whole template
$samson->render();

After being rendered, it will change the above variables to below.

<!DOCTYPE HTML>
<html lang="en">
<head>
	<title>Welcome to Samson!</title>
</head>

<body>
	<h1>Tuccinator</h1>
	<div id="testElement">Here is a test div.</div>
	<p>password</p>
	</br>
	<form action="" method="POST">
		<label for="username">Username</label>
		<input type="text" id="username" name="username" />
		
		<label for="role">Role</label>
		<select name="role" class="form-control">
			<option value="admin">Admin</option>
			<option value="mod">Mod</option>
			<option value="user">User</option>
		</select>

		<label for="description">Description</label>
		<textarea name="description" class="sumo">
			I would really like to be an admin.
		</textarea>

		<button type="submit" name="submit" class="btn btn-primary">Create</button>
	</form>

	<footer>This is a footer.</footer>
</body>
</html>

Go try it out and see if you like it. I would very much benefit from the feedback!

samson's People

Contributors

tuccinator avatar

Watchers

AppleJuice avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.