Preprocessing HTML files - template-engine

I'm evaluating brunch build system for my needs. I need to make simple HTML preprocessing. So basically I need to produce several files with common headers and footers:
file1.html:
<html>
<head>
<title>Title1</title>
</head>
<body>
<div id="content">
<div id="header">...</div>
Page1
<div id="footer">...</div>
</div>
</body>
</html>
file2.html:
<html>
<head>
<title>Title2</title>
</head>
<body>
<div id="content">
<div id="header">...</div>
Page2
<div id="footer">...</div>
</div>
</body>
</html>
So either a simple include functionality or (preferrable) some kind of extends functionality. Ideally syntax should hide in comments so my IDE won't complain about non-HTML characters. I liked preprocess javascript library, but that's not necessary, of course.
Unfortunately I didn't find anything suited for that task in brunch. There's support for many HTML template engines, but they seem to generate JS functions. I need simple static HTML as a result, not JavaScript SPA.

I'm not certain there's any built-in solution to this yet, but if I was to go in the direction of HTML templates / partials, I'd look into either "after-brunch" and "before-brunch" plugins on NPM.
I don't know what's your program-language of choice for FileSystem manipulation (read, merge, write, etc.), but in theory you could use something like "before-brunch" to execute a batch / shellscript / or command of somekind to collect your HTML partials together into your file1.html, file2.html, ... before Brunch compiles and copies it to the public/ folder.
In case you're familiar with Haxe, here's a Gist that I shared a while ago. It's a post-process script to merge other files on specific lines of documents.
https://gist.github.com/bigp/90e38deeccc94145b033
Here's what an HTML document could look like:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DEMO</title>
<link rel="icon" href="data:;base64,=">
<style id="css" type="text/css" rel="stylesheet">
/* #MacroMerge: public/app.css */ //<--- Merges All CSS here..
</style>
</head>
<body>
<script type="text/javascript">
/* #MacroMerge: public/vendor.js, public/app.js */ //<--- And all JS here..
</script>
</body>
</html>
EDIT:
Almost forgot, here's how the brunch-config.coffee script would use the Haxe script with the after-brunch plugin:
plugins:
afterBrunch: [
"haxe -cp . --macro MacroMerge.html('app/index.template.html','public/index.html')"
]
Come to think of it... nothing stops you from taking this example and specifying HTML partials (or any file extension really, ex: *.txt, *.xml) wherever you need them. Again, might only be useful to you if you're familiar with Haxe. If not, it's open-sourced & free to download (http://haxe.org/download/).

Related

How to embed (old) Tweets into a Presentation?

In a presentation, I would like to create a Twitter Timeline with a few selected tweets. In the best case it should look as natural as if someone went to Twitter on his own computer or saw it on his smartphone.
I am using LateX Beamer for the rest of the slides, but I am flexible if it is another software that allows to export a PDF/Image that I could include in LateX.
(There are a lot of tools on how to integrate live Tweets in PowerPoint / Keynote , but I want to do it for historical tweets and will be offline during the presentation.)
I was trying the following:
- Simply take screenshots of the tweet and arrange them within LateX or PowerPoint. Looks ok but not super nice, and quite cumbersome to do.
Since it is possible to extract an html of a tweet to embed on a homepage, I thought of doing this for the presentation. Unfortunately, I don't know much of html / reveal.js presentations.
Has anybody found a good solution for a similar problem?
EDIT:
I started reveal.js and found the following plugin: https://github.com/rajgoel/reveal.js-plugins/tree/master/embed-tweet
I was then trying to apply it and followed the intros to reveal.js and also saved the source code of the plugin into my plugin folder for reveal.js. However, it does not work (=Returning a blank page in the html). Can anybody point me to my error? (I am completely new to reveal.js / html so sorry if it is basic?)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Reveal.js 3 Slide Demo</title>
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/default.css" id="theme">
<!--Add support for earlier versions of Internet Explorer -->
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<!-- Wrap the entire slide show in a div using the "reveal" class. -->
<div class="reveal">
<!-- Wrap all slides in a single "slides" class -->
<div class="slides">
<!-- ALL SLIDES GO HERE -->
<!-- Each section element contains an individual slide -->
<section>
<div class="tweet" data-src="https://twitter.com/marcfbellemare/status/972558957645631488"></div>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Required, even if empty.
Reveal.initialize({
// ...
dependencies: [
// ...
{ src: 'plugin/embed-tweet/embed-tweet.js' },
// ...
]
});
</script>
</body>
</html>

Laravel 5 proper way to require CSS file from view

I've got specific Form component, which is declared as
Form::component('fcRadio', 'components.form.fcradio', ['name', 'options', 'selected' => null]);
and used as
{{ Form::fcRadio('name', $options }}
What I want is somehow attach custom CSS file, so if the page fires this component at least once, the desired CSS file is included to the <head> of my document.
For example, in Joomla it was like
$this->document->addStylesheet('my_awesome_style.css');
Is there any way to achieve the same in Laravel?
UPD:
I've extended the answers below a bit to let it add multiple styles from multiple templates. Finally, it looks like this:
#section('styles')
#parent
{{HTML::style('css/fcradio.css')}}
#stop
It works fine, but if I use the component twice per page, style is also adds twice. How can I allow multiple but unique entries?
So this is typically how I deal with it:
In your folder: resources/views I create a folder called layout. This folder handles the templates for all my pages.
Then I create a file called default.blade.php. In it I put the bulk of the HTML code. Here's an example of how default.blade.php could look (slimmed down, obviously)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
#yield('title')
</title>
<link rel="stylesheet" href="{{ asset('css/main.css') }}">
<!-- Additional per-page css -->
#yield('css')
</head>
<body>
#yield('content')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="js/script.js"></script>
<script src="{{ asset('js/bootstrap.min.js') }}"></script>
<!-- Include per-page JS -->
#yield('js')
</body>
</html>
Right, so essentially what we have so far is the #yield() and asset() helpers.
#yield() is special blade syntax that Laravel uses to say, "Okay. Any time a blade view that is inheriting THIS master template calls the section named in this #yield() I will display that content right here.
asset() is a nifty little helper that basically appends your URL structure onto the string you pass it. So if your url is http://MyGreatSite.com and you use asset('js/script.js') it will spit out a fully qualified URL that will work anywhere on the site (http://MyGreatSite.com/js/script.js). asset() is great because you can use it in blade templates that will get sent out as an email and all of the files will work in an email inbox because they are absolute links.
Right. So now we have this master template and we need to use it. So what I do is create another view in the resources/views directory. Lets say we're doing a contact page. I would make contact.blade.php. Now I want to inherit that master template we created. So we do that like so:
#extends('layout.default)
#section('css')
<link rel="stylesheet" href="{{ asset('css/contact.css') }}">
#stop
#section('title')
Contact Us
#stop
#section('content')
<h1>Contact us</h1>
<p>
Contact us via email: contact#mygreatsite.com
</p>
#stop
#section('js')
<script src="{{ asset('js/contact-form.js') }}"></script>
#stop
Okay, so, first things first. At the very top we tell this blade file that we want to use the template we just made. We use the blade helper #extends() and pass it the path to our view relative to the views directory separated by periods.
Next, we want to create the sections that correspond to the template. We do that by opening the section with #section() and passing the name of the section we want to push this block of content to. We write our content and then we close the section by using #stop. Pretty simple. For images, css, or js, we simply use the asset() helper again.
I know it's a little long-winded, but hopefully that helps and explains the process a little better.
tl;dr: Use #yield(), #section(), and asset().
So I think I understand what you are saying.
In your blade layout file create a section inside the head:
<head>
#yield('componentcss')
</head>
And in the component do:
#section('componentcss')
{{HTML::style('css/fcradio.css')}}
#stop
You could also just include the css but I wouldn't advise this:
#section('componentcss')
<style>
.exampleclass {text-align:center;}
</style>
#stop
Hopefully I have understood you correctly.
I've finally found a bit tricky but working solution:
#hasSection('fcRadioStyle')
#else
#section('fcRadioStyle')
{{Html::style('css/components/fcradio.css')}}
#stop
#section('styles')
#yield('fcRadioStyle')
#append
#endif
This makes by Form::fcRadio append this style only once

Datalogic Skorpio scanner javascript

We have a browser-based solution that we want to integrate with Datalogic scanners.
We will be using the locked down browser as our primary interface.
We've got as far as configuring the scanner and can confirm that it is decoding our Code 39 barcodes.
We've set up a test page that is supposed to take the scanned code and dump it in a text area.
The test page is
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>TEST</title>
<meta http-equiv="DL_Code_39" content="Enable">
<meta http-equiv="DL_Scan" content="Javascript:ValidateInput()">
<script language="javascript" type="text/javascript">
function ValidateInput(n){
document.getElementById("sku").value+=";"+n;
};
</script>
</head>
<body>
<form method="post" name="fTest">
<textarea rows="5" cols="15" name="sku" id="sku"></textarea><br>
<input type="submit" value="go">
</form>
</body>
</html>
When we scan, the javascript call is firing, but returning undefined.
If we give the javascript function call a variable (something not done in the documentation) it does not fire
We must be missing something simple but there is no sample code in the DL documentation and google can find nothing else either.
Any help would be greatly appreciated.
I have always found javascript support to be flaky on Windows CE. I assume this is what the data logic scanner is running on?
I would normally configure the scanner to act as a keyboard, that way you can use standard html forms and handle the logic server side. I haven't got a Scorpio to test with but the Falcons have this ability under encoding options.
You can also set a prefix and suffix that the scanner will append to the scanned barcode. In your case it looks like this might be ';\n'
I have solved this on my own
The problem in this case is one of the reasons why developers drink too much.
The problem is the name of the example javascript function described in the documentation
This code works perfectly
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>TEST</title>
<meta http-equiv="DL_Triggers" content="Enable">
<meta http-equiv="DL_Code_39" content="Enable">
<meta http-equiv="DL_Scan" content="Javascript:PassSKU">
<script type="text/javascript">
function PassSKU(n){
if (n === undefined) {
n = 0;
}
document.getElementById("sku").value+=";"+n;
};
</script>
</head>
<body>
<form method="post" name="fTest">
<textarea rows="5" cols="20" name="sku" id="sku"></textarea><br>
<input type="submit" value="go">
</form>
</body>
</html>
The only thing I changed was to rename my function call from ValidateInput() (the function name given in the documentation which I copied and pasted) to PassSKU
So
<meta http-equiv="DL_Scan" content="Javascript:ValidateInput">
This does not work
<meta http-equiv="DL_Scan" content="Javascript:AnyOtherFunctionName">
This works fine
WHY this fixed the problem is a topic for another time.
In case someone comes across this question while searching for information on getting a Datalogic scanner to work with a web form, I've posted a working solution here: stackoverflow: "Datalogic Falcon X3 - Barcode Scanner"

Processing (.pde) sketch on the web with multiple classes

I am trying to put processing sketches with multiple .pde files(tabs or classes) on my website, but it does not seem to be working. You should be able to click and make balls appear that bounce around the screen. However, it does not seem to be registering that there is a second .pde file, and therefore, the sketch does not work fully.
<!DOCTYPE html>
<html>
<head>
<script src="processing.min.js"></script>
<title>Projects</title>
<meta charset="UTF-8"/>
<link rel="stylesheet" href="../css/projects.css"/>
</head>
<body>
<canvas data-processing-sources="ball/Ballbounces.pde" ></canvas>
<canvas data-processing-sources="ball/Ball.pde" ></canvas>
</body>
</html>
http://willhay.io/processing/
A regular one file sketch works fine, I think it has something to do with the fact that this sketch has a ball.pde class for the balls that are supposed to appear.
Figured it out, it was as simple as typing the other .pde file you wanted to load after the one you chose like so (with a space):
<canvas class="project" data-processing-sources="ball/Ballbounces.pde ball/Ball.pde" ></canvas>

Does Spring has something like Play #{extends}?

#{extends} is very helpful and easy to use in Play framework to extend layouts and manage layouts in views, but I'm wondering if Spring also has something similar to this?
I'd really appreciate if anyone has same kind of code for Spring.
In following Play example I am extending main.html by index.html which means index.html will use main.html as its layout:
/* views/main.html : */
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
...
<div id="content">
#{doLayout/}
</div>
...
</body>
</html>
/* views/Application/index.html : */
#{extends "main.html"/}
<h1>Home</h1>
...
If you are using Velocity, read http://sebastienayotte.wordpress.com/2009/03/20/using-velocity-layout-in-spring/
If you use freemarker, see http://richardbarabe.wordpress.com/2009/03/19/freemarker-a-brief-example/
If you want the behaviour exactly like play's groovy template, you can try Rythm template engine, read this and this.

Resources