I am using Jade just to include other sections in my HTML files using include. This works fine if I write Jade syntax instead of HTML syntax. But in my case, I need to write HTML syntax only. I am trying to use jade just for include only.
From this link, I found that we can write HTML by including . or | in the code. So, to test this out, I wrote the code like this:
File index.jade
div.main.
<div class="wrapper">
include header
</div>
As you can see in above code, I added . as suffix to the Jade syntax line, i.e., div.main., which lets me write HTML from next line.
File header.jade
<header></header>
But this isn't working. The rendered HTML looks like this:
File index.html
<div class="main">
<div class="wrapper">
include header
</div>
</div>
If I don't use . and follow the Jade syntax, everything works fine. But in my case, I really need to write in HTML, but not in Jade.
Is there a workaround to make the include work inside the HTML syntax?
Well, it is possible to do what you want, but I am not sure if Jade is the best option.
Note: In Jade, every line which starts with < is considered plain text, so there is no need to use dot or | to write html tags.
Here is a working example of what you want:
a.jade
div.main
<div class="wrapper">
include b.jade
</div>
b.jade
<div class="b">I am content from b.jade</div>
and after compilation of a.jade we get:
a.html
<div class="main">
<div class="wrapper">
<div class="b">I am content from b.jade</div>
</div>
</div>
This code was tested and it works 100% with the latest version of Jade, but It works only when you don't increase indentation level. For example, the following code will not work:
div.main
<div class="wrapper">
include b.jade
</div>
On compilation it will throw: unexpected token "indent", and the error itself:
div.main
<div class="wrapper">
include b.jade
^^ extra indent "tab"
</div>
The same is true for nested plain HTML too, so the following:
div.main
<div class="wrapper">
<div class="foo">
include b.jade
</div>
</div>
will also throw this error: unexpected token "indent", and the errors:
div.main
<div class="wrapper">
<div class="foo">
^^
include b.jade
^^^^
</div>
^^
</div>
You can write code like this:
div.main
| <div class="wrapper">
| <div class="foo">
| <div class="bar">
include b.jade
| </div>
| </div>
| </div>
and assuming that we already have that b.jade, it will be compiled into:
<div class="main">
<div class="wrapper">
<div class="foo">
<div class="bar"><div class="b">I am content from b.jade</div></div>
</div>
</div>
</div>
But note where I placed that include b.jade, exactly one tab has been added in comparison with last Jade command div.main (so included file will be nested into .main div), and you should follow that indent rule if you want your code to work.
Alternative solution
As I wrote at the beginning, Jade is not the best option in your case. I would use another server side language to do what you want.
Here is a basic algorithm:
Write your HTML files in plain HTML (.html) and as an include use a custom tag like <include b.html>
Create a master file using a server side language which will load and process your HTML files and will replace your custom tags with actual content from these files
Save output to a new file and use it.
Here is an example written in PHP:
master.php
<?php
$main_file = "a.html";
$content = file_get_contents($main_file);
$content = preg_replace_callback(
'!<include\s+([^>]+)>!',
function ($m) {
return file_get_contents($m[1]);
}, $content
);
file_put_contents("bundle.{$main_file}", $content);
Now HTML files:
a.html
<div class="main">
<div class="wrapper">
<include b.html>
</div>
</div>
b.html
<div class="b">foobar</div>
Now after we will execute master.php we will get bundle.a.html with the following content:
bundle.a.html
<div class="main">
<div class="wrapper">
<div class="b">foobar</div>
</div>
</div>
I get stuck by the same problem. Jade requires me to use no indent in the plain HTML. But if you change the header.jade to header.html, it will work.
Related
I am building plone 5 theme and have a question about replacing nested html elements using rules.xml. Please see the structure then read the question at the end.
Plone5 theme file structure
index.html
footer.html
header.html
header_1.html
header_2.html
rules.xml
index.html
This is my default theme page. I have to div that I want them to be replaced with other elements.
I am doing so in the rules.xml.
...
<body>
<div id="header"/>
<div>my content. does not need to be replaced. static</div>
<div id="footer"/>
</body>
...
footer.html
<div id="footer">
<div>This is your footer</div>
</div>
header.html
<div id="header">
<div>This is your header</div>
<div id="header_1"/>
<div id="header_2"/>
</div>
header_1.html
<div id="header_1">
<div>This is part 1 of your header</div>
</div>
header_2.html
<div id="header_2">
<div>This is part 2 of your header</div>
</div>
rules.xml
<rules
xmlns="http://namespaces.plone.org/diazo"
xmlns:css="http://namespaces.plone.org/diazo/css"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- in here I am replacing the footer. this works because there is not other elements inside
footer.html that needs to be replaced. -->
<replace
href="path/to/footer.html"
css:theme="#footer"
css:content="#footer"
method="document"
/>
***
RULE FOR REPLACING
header
header_1
header_2
</rules>
Question
How can i write the replace rule for the header to replace first the parent which is header and then replace header_1 and header_2 inside header parent.
I tried different ways to do this but I was not able to replace the elements inside header.html.
Basically, rules.xml has load header and replace its elements then see if apply the the rules on the element that was just replaced.
i don't think that is gonna work. you need to define one template with theme="index.html" and the diazo compiler will build xslt rules in there. then you can use the rules to get content from Plone and even use the href attribute to get content from different urls. with inline rules you could inject parts into your theme or better you could drop parts which are not wanted. So i would put you header / footer variants into the main theme template and use conditional drop rules to clean it out. you also can use different theme templates depending on current path or other selectors from the content side. in Plone you have useful css classes in the body tag for that.
I want to save output of HTML div as an image. Can we do it in rmarkdown or in R using any package like htmltools?
Like
<div class="w3-container w3-teal">
<h1>My Header</h1>
</div>
Found the solution at Convert html output to image
See also
https://cran.r-project.org/web/packages/webshot/webshot.pdf
I am building a page preloader with ReactJS. The Codepen snippet I am using is written in HTML and I need help converting it into HTML ready for React.
Part of the snippet
<div class="socket">
<div class="gel center-gel">
<div class="hex-brick h1"></div>
<div class="hex-brick h2"></div>
<div class="hex-brick h3"></div>
</div>
</div>
Conversion to React
<div className={s.socket}>
<div className={s.gel s.center-gel}>
<div className={s.hex_brick s.h1}></div>
<div className={s.hex_brick s.h2}></div>
<div className={s.hex_brick s.h3}></div>
</div>
</div>
So I have replaced hyphens with underscores, used curly brackets instead of quotes, and added Name to the class. However, I don't know how to add the second div modifier (for example in div gel there is element center-gel). When a second element is added to a React div, it fails to compile.
React does not allow these second div elements. After testing, my loading animation does not look correct if I separate out the elements, the structure needs to stay the same.
Snippet Used
When you write JSX code in React you are actually writing JavaScript code. Let's say you put those expression into a variable like so:
var styles = s.gel s.center-gel;
It just doesn't make any sense in JS. You need to write a valid expression like this:
var styles = [s.gel, s['center-gel']].join(' ');
Keeping that in mind, your code should work this way:
<div className={s.socket}>
<div className={[s.gel, s['center-gel']].join(' ')}>
<div className={[s.hex_brick, s.h1].join(' ')}></div>
<div className={[s.hex_brick, s.h2].join(' ')}></div>
<div className={[s.hex_brick, s.h3].join(' ')}></div>
</div>
</div>
I'm using Visual Studio 2012 for an MVC web app using code first method with EF 5.0.
I have written the following code to make a modal window appear at some point:
<div id="mod" style="display:none;">
<div id="mod-container">
<div id="mod-close"><img src="~/Content/icons/close.png" title="close" onclick="$('#mod').fadeOut();"/></div>
<div id="mod-content"></div>
</div>
</div>
If works fine, exept that the image <img src="~/Content/icons/close.png" [...] /> cannot be found by the browser which thinks its URL is
http://localhost:49895/Class1/Home/~/Content/icons/close.png
To be precise, every code under my div's got broken URL. If I put my image above the div's it's displaying correctly with the following URL:
http://localhost:49895/Content/icons/edit.png
Do you have an idea where i messed things up?
Edit2: example (after problem being resolved)
This works:
<img src="~/Content/icons/close.png" title="close" onclick="$('#mod').fadeOut();"/>
<!-- comment containing a quote ' -->
<div id="mod" style="display:none;">
<div id="mod-container">
<div id="mod-close"></div>
<div id="mod-content"></div>
</div>
</div>
This doesn't work:
<!-- comment containing a quote ' -->
<div id="mod" style="display:none;">
<div id="mod-container">
<div id="mod-close"></div>
<div id="mod-content"></div>
</div>
</div>
<img src="~/Content/icons/close.png" title="close" onclick="$('#mod').fadeOut();"/>
Could be a bug in the new Razor 2.0 tilde parsing, or you've mucked up your html by missing a quotation mark or something. Try using the more explicit way of resolving urls
<img src="#Url.Content("~/Content/icons/close.png")" />
If that works then it suggests a razor bug, if it doesn't then your html is probably broken somehow but the extra # symbol may be enough for the parser to kick in and tell you what is wrong.
~ is an asp code element, not HTML. As such it doesn't get rendered by the HTML.
Try wrapping your src with #Url.Content
<img src="#Url.Content("~/Content/icons/close.png")" />
After accidentally using a CSS selector in an HTML template I started wondering if there is a template language or an extension to one that would allow this syntax, and whether it would be useful. So instead of writing this:
<div id="mydiv">
<div class="first column">1</div>
<div class="second column">2</div>
</div>
We could write it like:
<div#mydiv>
<div.first.column>1</div>
<div.second.column>2</div>
</div>
Does something like this exist?
Maybe you mean something like Jade?
It is an HTML preprocessor.
The following:
doctype 5
html(lang="en")
head
title= pageTitle
script(type='text/javascript')
if (foo) {
bar()
}
body
h1 Jade - node template engine
#container
if youAreUsingJade
p You are amazing
else
p Get on it!
Will be translated to:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Jade</title>
<script type="text/javascript">
if (foo) {
bar()
}
</script>
</head>
<body>
<h1>Jade - node template engine</h1>
<div id="container">
<p>You are amazing</p>
</div>
</body>
</html>
Also, it is not exactly what you're asking, but you may like Zen Coding. It is a plugin to code HTML at high-speed. GIF showing what it does:
It's basically:
Write pseudo-html.
Hit the shortcut.
Get full HTML.
?????
Profit!
You should check with your editor if it can support this. FWIW, I use this in VIM and it's awesome.
Maybe haml will fit your needs? It looks very similar.
There's a tool that uses a similar (though not identical) syntax called Zen Coding. You type this in your Zen Coding enabled editor:
div#page>div.logo+ul#navigation>li*5>a
... and get it expanded to:
<div id="page">
<div class="logo"></div>
<ul id="navigation">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
This differs from what you describe in that it doesn't require a preprocessor to run the template, it's just an editor helper to compose the final HTML. It may suit your needs or not.