I created jinja module as example, which looks like this
{% from 'snippet.j2' import module with context %}
{% call module() %}
logic [$size(din.data)-1 : 0] res;
assign res = din.data * din.data;
{% if params['half'] %}
assign dout.data = res / 2;
{% else %}
assign dout.data = res;
{% endif %}
assign din.ready = dout.ready;
assign dout.valid = din.valid;
{% endcall %}
How should I use this module inside PyGears?
Okay, I think this should work.
If I understood correctly you are trying to create a Jinja template for a module that will multiply with 1/2 (in other words divide by two). First of all, make sure your Jinja file and module are named the same (this is a must so PyGears would know which Jinja template to use).
Having all this in mind let's say our module name is mulh
Python file would be something like this:
from pygears import gear, Intf, reg
from pygears.typing import Uint
from pygears.hdl import hdlgen
#gear
def mulh(din: Uint,*,half=False)->b'din*din':
pass
mulh(Intf(Uint[8]))
hdlgen('/mulh', outdir='.')
This code will call your Jinja file and the HDL output would look like something like this:
module mulh
(
input logic clk,
input logic rst,
dti.consumer din, // u8 (8)
dti.producer dout // u16 (16)
);
typedef logic [7:0] din_t; // u8
typedef logic [15:0] dout_t; // u16
din_t din_s;
dout_t dout_s;
assign din_s = din.data;
assign dout.data = dout_s;
logic [$size(din.data)-1 : 0] res;
assign res = din.data * din.data;
assign dout.data = res;
assign din.ready = dout.ready;
assign dout.valid = din.valid;
endmodule
To make it easier to picture all of this I made this picture bellow
I am trying to place a golang array (also slice, struct, etc.) to HTML so I can use array element in HTML Element content when returning HTML from golang gin web framework. Another problem is how to render these data with loop? Such as Flask jinja
works in this way.
{% block body %}
<ul>
{% for user in users %}
<li>{{ user.username }}</li>
{% endfor %}
</ul>
Usually you have a folder with template files so first you need to tell the gin where these templates are located:
router := gin.Default()
router.LoadHTMLGlob("templates/*")
Then in handler function you simply pass template name the data to HTML function like this:
func (s *Server) renderIndex(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", []string{"a", "b", "c"})
}
In index.tmpl you can the loop the data like this:
{{range .}}
{{.}}
{{end}}
The . is always the current context so in first line . is the input data and inside the range loop . is the current element.
Template example: https://play.golang.org/p/4_IPwD3Y84D
Documentation about templating: https://golang.org/pkg/text/template/
Great examples: https://astaxie.gitbooks.io/build-web-application-with-golang/en/07.4.html
The Problem
I am building a Jekyll site with the minima theme to publish some tutorial online. The tutorial pages contain many code snippets, for example:
```javascript
/* Global scope: this code is executed once */
const redis = require('redis');
const host = <HOSTNAME>;
const port = <PORT>;
const password = <PASSWORD>;
...
```
I would like to add a "copy to clipboard" button to each code snippet (example), but not sure what's the right way to do it in Jekyll.
What have I tried
Using clipboardjs.com. It requires a unique ID for each snippet, and I'm not sure how to implement this in Jekyll/Markdown.
STFW
My question
How can I add a "Copy to Clipboard" button for code snippets in Jekyll?
Manually generate id's for each block of code with kramdown's Block Inline Attribute Lists, adding {: #code-example-1} after it.
In your example:
```javascript
/* Global scope: this code is executed once */
const redis = require('redis');
const host = <HOSTNAME>;
const port = <PORT>;
const password = <PASSWORD>;
...
```
{: #code-example-1}
That will generate:
<div id="code-example-1" class="language-javascript highlighter-rouge">
....
</div>
using jquery
Code blocks use thecode html element, if we detect it, then we load the js, traverse all code elements adding a custom id, and a button to copy their content. Finally initialize the Clipboard buttons.
{% if page.content contains "code" %}
<script>
<!-- clipboard.js code -->
</script>
{% endif %}
// get all <code> elements
var allCodeBlocksElements = $( "code" );
allCodeBlocksElements.each(function(i) {
// add different id for each code block
// target
var currentId = "codeblock" + (i + 1);
$(this).attr('id', currentId);
//trigger
var clipButton = '<button class="btn" data-clipboard-target="#' + currentId + '"><img src="https://clipboardjs.com/assets/images/clippy.svg" width="13" alt="Copy to clipboard"></button>';
$(this).after(clipButton);
});
new Clipboard('.btn');
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/clipboard#1/dist/clipboard.min.js"></script>
<code>print("Club Nacional de Football")</code>
<br>
<code>print("is a sports institution")</code>
<br>
<code>print("from Uruguay")</code>
Let's use includes.
tuto.md
---
front matter things here
---
{%- capture code -%}
/* Some js code */
const redis = require('redis');
const host = <HOSTNAME>;
{%- endcapture -%}
{% include code_snippet.md code=code language='javascript' %}
{%- capture code -%}
# Some ruby code
t = Time.now
t.succ
{%- endcapture -%}
{% include code_snippet.md code=code language='ruby' %}
_includes/code_snippet.md
{% assign code = include.code %}
{% assign language = include.language %}
``` {{ language }}
{{ code }}
```
{% assign nanosecond = "now" | date: "%N" %}
<textarea id="code{{ nanosecond }}" style="display:none;">{{ code | xml_escape }}</textarea>
<button id="copybutton{{ nanosecond }}" data-clipboard-target="#code{{ nanosecond }}">
Copy to clipboard
</button>
<script>
var copybutton = document.getElementById('copybutton{{ nanosecond }}');
var clipboard{{ nanosecond }} = new Clipboard(copybutton);
clipboard{{ nanosecond }}.on('success', function(e) {
console.log(e);
});
clipboard{{ nanosecond }}.on('error', function(e) {
console.log(e);
});
</script>
I am using twig to render a view and I am using the striptags filter to remove html tags.
However, html special chars are now rendered as text as the whole element is surrounded by "".
How can I either strip special chars or render them, while still using the striptags function ?
Example :
{{ organization.content|striptags(" >")|truncate(200, '...') }}
or
{{ organization.content|striptags|truncate(200, '...') }}
Output:
"QUI SOMMES NOUS ? > NOS LOCAUXNOS LOCAUXDepuis 1995, Ce lieu chargé d’histoire et de tradition s’inscrit dans les valeurs"
If it could help someone else, here is my solution
{{ organization.content|striptags|convert_encoding('UTF-8', 'HTML-ENTITIES') }}
You can also add a trim filter to remove spaces before and after.
And then, you truncate or slice your organization.content
EDIT November 2017
If you want to keep the "\n" break lines combined with a truncate, you can do
{{ organization.content|striptags|truncate(140, true, '...')|raw|nl2br }}
I had a similar issue, this worked for me:
{{ variable |convert_encoding('UTF-8', 'HTML-ENTITIES') | raw }}
I was trying some of, among others, these answers:
{{ organization.content|striptags|truncate(200, true) }}
{{ organization.content|raw|striptags|truncate(200, true) }}
{{ organization.content|striptags|raw|truncate(200, true) }}
etc.
And still got strange characters in the final form. What helped me, is putting the raw filter on the end of all operations, i.e:
{{ organization.content|striptags|truncate(200, '...')|raw }}
Arf, I finally found it :
I am using a custom twig filter that just applies a php function:
<span>{{ organization.shortDescription ?: php('html_entity_decode',organization.content|striptags|truncate(200, '...')) }}</span>
Now it renders correctly
My php extension:
<?php
namespace AppBundle\Extension;
class phpExtension extends \Twig_Extension
{
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('php', array($this, 'getPhp')),
);
}
public function getPhp($function, $variable)
{
return $function($variable);
}
public function getName()
{
return 'php_extension';
}
}
2022 update | tested with Drupal 8.6.16
I tried the top voted recommendation. It worked ok with some symbols but not with others.
raw filter seems to be working ok with all special characters.
like so
{{ organization.content|striptags|raw }}
The best way to do this is :
{{ organization.content|striptags|truncate(200, '...')|raw }}
With |raw always at the end.
Don't use convert_encoding('UTF-8', 'HTML-ENTITIES'), you will encounter iconv issues.
When I thought none of the above answers were working for me (convert_encoding running into iconv() issues in Drupal 9, and I thought raw, but because applying it on the argument side of an {% embed %} — as opposed to in the embedded template itself — didn't seem to help), another approach that seemed to work for me was:
{% autoescape false %}
{{ organization.content|striptags|truncate(200, '...') }}
{% endautoescape %}
with that false part being key.
I had the same problem, I resolved it byt this function below, using strip_tags.
<?php
namespace AppBundle\Extension;
class filterHtmlExtension extends \Twig_Extension
{
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('stripHtmlTags', array($this, 'stripHtmlTags')),
);
}
public function stripHtmlTags($value)
{
$value_displayed = strip_tags($value);
return $value_displayed ;
}
public function getName()
{
return 'filter_html_extension';
}
}
I want to make my GAE application webapp2 compatible.
This code worked great with webapp:
insert = '<p><font color="red"><b>some text</b></font></p>'
template_values = {
'insert': insert,
...
}
path = ...
self.response.out.write(template.render(path,template_values))
The content of the variable insert was just put into the web page output by webapp. Now the content of the variable is "analyzed" by webapp2 and the content is changed when it is inserted in the webpage.
webapp2 inserts this:
<p><font color="red"><b>some text</b></font></p>
How can I go back to the old behavior?
Thanks for any help.
Have a look at
safe : https://docs.djangoproject.com/en/dev/ref/templates/builtins/#safe &
autoescape : https://docs.djangoproject.com/en/dev/ref/templates/builtins/#autoescape.
Eg:
{{ insertHTML|safe }} OR
{% autoescape off %}{{ inserHTML }}{% endautoescape %}