Swiftpack.co - Package - ucotta/BrilliantTemplate


BrilliantTemplate is a template processor for Swift to process HTML5.

It was created with designers in mind. BrilliantTemplate avoid dirty code by using tags and attibutes for variables and control structures.

Merging the data with the template is really easy: just prepare a dictionary with all values and then, use BrilliantTemplate with your html5 template.

This readme wants to show what BrilliantTemplate can do, is not a reference manual, you can get more information and usage guide in the wiki of this project in github: https://github.com/ucotta/BrilliantTemplate

Here is a dictionary example:

let data: [String: Any?] = [
	"lang": "en",
	"title": "web title",
	"keywords": "one, two, three",
	"description": "meta description",
	"links": [
		["link": "https://github.com/ucotta", "title": "ucotta's repositories"],
		["link": "https://github.com/ucotta/BrilliantTemplate", "title": "this repository"],
	"user": [
		"name": "username",
		"id": 10
	"javascriptVars": "var id=10;",
	"amount": 1020.30,
	"total": 20

The template for example.html

<html bid-lang="lang">
	<title tid="title:upper:cap"></title>
	<meta name="keywords" bid-content="keywords:lower">
	<meta name="description" bid-content="description">

	<script jsid="javascriptVars">
		// this javascript will be replaced by the javascriptVars data
		// The designer will use this ids in his/her editor.
		var ids = [1,2,3,4];
	<include file="menu.html"></include>

	<h1 tid="title:cap" bid-data-val="importe:currency/en_US">Title example</h1>

		<div tid="amount:currency/en_US">show currency in US</div>
		<div tid="amount:decimal/es_ES/4">show decimal number with thousang separator in spaniard format</div>
		<div tid="total:decimal/es_ES/4">shame case with a Int number.</div>
		<div tid="total">Default not formatted</div>

	<div tid="links">
		<p><a bid-href="link" bid-title="title" tid="title">some document...</a></p>

And this is menu.html

	<div class="coco">
		<!-- comment in menu -->
		<div class="menu">
			this is the menu!!

This is the call to library:

import BrilliantTemplate

var tc = BrilliantTemplate("example.html", path: "/var/www/templates")
print(tc.getHTML(data: data, ))

And this will be the result for this code:

<html lang="">
	<title>web title</title>
	<meta name="keywords" content="one, two, three">
	<meta name="description" content="meta description">

		var id=10;
	<div class="coco">
		<!-- comment in menu -->
		<div class="menu">
			this is the menu!!

	<h1 data-val="">Web Title</h1>

		<p><a href="https://github.com/ucotta" title="ucotta&apos;s repositories">ucotta&apos;s repositories</a></p>
		<p><a href="https://github.com/ucotta/BrilliantTemplate" title="this repository">this repository</a></p>

The final HTML was retabuled for easy reading.


Create this files in a new directory:


import PackageDescription

let package = Package(
	name: "MyTest",
	targets: [],
	dependencies: [
		.Package(url: "https://github.com/ucotta/BrilliantTemplate.git", majorVersion: 0)
	exclude: ["BrilliantTemplateTests"]


import Foundation
import BrilliantTemplate

// This data will be merged
var data: [String: Any] = [
	"title": "This is the title",
	"welcome": "this is your first example!!",
	"more": "and you can do a lot of stuff",
	"buttonColor": "btn-blue btn-sm btn"

// Load a raw HTML 
let URLINDEX = "https://raw.githubusercontent.com/ucotta/BrilliantHTML5Parser/master/examples/index.html"

if let url = URL(string: URLINDEX) {
	let html = try String(contentsOf: url)

	let bt = BrilliantTemplate(html: html, data: data, path: "/tmp")

	print("Before --------\n")

	print("After ---------\n")

} else {
	print("error in URL \(URLINDEX)")

Create the project, open it

swift package generate-xcodeproj && open MyTest.xcodeproj

Now select the correct scheme and run!.

More information in the Wiki of this project



Stars: 4
Help us keep the lights on


0.95.0 - Oct 13, 2017

Removed some unnecessary methods since then strings "replace" and "contains" works perfect on Linux.

This version is compatible with only with Swift 4.0+

0.94.2 - Apr 20, 2017

TID attributes now available in String, Date and Numbers.

0.94.1 - Apr 20, 2017

Not documented yet.

0.92.1 - Feb 27, 2017

0.91.0 - Jan 9, 2017

Bid system allow to use multiple aid attribute in the same tag.