UTF8 Character Encoding using Twig - Symfony2 - utf-8

I'm getting data from a database and it's save in the table like this:
<p>Com o objetivo de conscientizar a população
I used the filter striptags to remove the HTML tags but I can't change the text to UTF-8!
All my files are UTF-8 and they all have
<meta charset="UTF-8" />
I tried
{{my_text|convert_encoding('UTF-8', 'ISO-8859-1')}}
and
{{my_text|raw|convert_encoding('UTF-8', 'ISO-8859-1')}}
My code is:
{% for noticia in noticias%}
<a href="noticias/{{noticia.id}}">
<div class="noticia">
<img src='assets/temp/op1.jpg'>
<h4 class="title">{{noticia.titulo|striptags}}</h4>
<p class="text">{{noticia.texto|striptags|slice(0,180)|join(' ') ~ '…'}}</p>
</div>
</a>
{% endfor %}
Can someone help me? Should I ask the database guy to fix it in the database?

Related

validate_on_submit is not working using WTF Forms

When I registered on the page, I see that are the values getting inserted into the database and it is redirecting me to the Login function as mentioned in the code, but I don't understand why the login page itself is getting redirected every time I tried to login, instead, it should get redirected to the chat function.
I see that index function is working clearly
and also the values are getting inserted into database
But when I tried to login it's not getting redirected to the chat page
When I printed "login_form.validate_on_submit", it is always resulting false, so I could see that there is something wrong with the validate_on_submit function, but I'm not getting what it is.
This is my Login Function inside application.py:
#app.route('/login',methods=["GET","POST"])
def login():
login_form=LoginForm()
print(login_form)
if login_form.validate_on_submit():
user_object=User.query.filter_by(username=login_form.username.data)
login_user(user_object)
return redirect(url_for("chat"))
return render_template("login.html", form=login_form)
This is WTForm_Fields.py:
def invalid_credentials(form, field):
""" Username and password checker """
password = field.data
username = form.username.data
# Check username is invalid
user_data = User.query.filter_by(username=username).first()
print(user_data)
if user_data is None:
raise ValidationError("Username or password is incorrect")
# Check password in invalid
elif not pbkdf2_sha256.verify(password, user_data.hashed_pswd):
raise ValidationError("Username or password is incorrect")
class LoginForm(FlaskForm):
"""login form"""
username=StringField('username_label',validators=[InputRequired(message="username required")])
password=PasswordField('password_label',validators=[InputRequired(message="Password Required"),invalid_credentials])
This is login.html:
{% from 'form_helper.html' import displayField %}
{% extends 'prelogin-template.html'%}
{%block title%}Login{%endblock%}
{%block content%}
<h2>Login now!</h2>
<p>Enter your username/Password to start!!</p>
<form action="{{ url_for('login') }}",method='POST'>
{{displayField(form.username,"Username",autocomplete='off',autofocus=true)}}
{{displayField(form.password,"Password")}}
<div class="form-group">
<input type="submit" value="Login" class="btn btn-warning">
</div>
{{ form.crsf_token }}
</form>
{%endblock%}
This is prelogin-template.html:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>{%block title%}{%endblock%}-Let's Chat </title>
</head>
<body>
{% with messages=get_flashed_messages(with_categories=true) %}
{% if messages %}
Category:{{ messages[0][0] }}
{{messages[0][1]}}
{% endif %}
{% endwith %}
{%block content%}
{%endblock%}
</body>
</html>
This is form_helper.html:
{% macro displayField(fieldName,placeholderValue) %}
<div class='form-group'>
{{fieldName(class="form_control",placeholder=placeholderValue,**kwargs)}}
<ul class="formError">
{% for error in fieldName.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
{% endmacro %}
The way I see it, there are no problems in your form_helper.html, prelogin-template.html, login.html and WTForm_Fields.py files. However, there is an issue in your Login Function. I think that Database query error has occurred. One suggestion is to simply add .first() at the end of the user object. flask_login sometimes does not log a user in when you provide flask_sqlalchemy.BaseQuery object as the parameter.
Try doing this in Login Function inside application.py:
#app.route('/login',methods=["GET","POST"])
def login():
login_form=LoginForm()
print(login_form)
if login_form.validate_on_submit():
user_object=User.query.filter_by(username=login_form.username.data).first() #<--add here
login_user(user_object)
return redirect(url_for("chat"))
return render_template("login.html", form=login_form)
This should smoothly log a user in.

How to embed html using plugin in Jekyll?

I have this problem I want to inject html into markdown file (blog post) but It's little bit long so I want to have plugin with parameters because it will change and I may add it multiple times. When search inject html I only found that you can insert html directly into markdown files, but I want to have one tag that will do this for me, to not duplicate the code, it will also be easier to update if Codepen decide to change the embed code.
This is the code I want to add, Codepen embed demo with button:
<div id="codepen">
<button class="btn" onclick="document.body.classList.toggle('sticky')">Dock</button>
<iframe height="265" scrolling="no" title="in browser sql terminal"
src="//codepen.io/jcubic/embed/dVBaRm/?height=265&theme-id=dark&default-tab=result"
frameborder="no" allowtransparency="true" allowfullscreen="true">
See the Pen <a href='https://codepen.io/jcubic/pen/dVBaRm/'>in browser sql terminal</a> by Jakub T. Jankiewicz
(<a href='https://codepen.io/jcubic'>#jcubic</a>) on <a href='https://codepen.io'>CodePen</a>.
</iframe>
</div>
I want to have params username and id (maybe also title and full name), what is the easiest way to create such plugin and how to use in my markdown file?
You don't need a plugin to do this.
You can use a Jekyll include.
example_page.html
---
layout: page
title: Codepen include example
codepen:
author: jcubic
name: Jakub T. Jankiewicz
id: dVBaRm
title: "in browser sql terminal"
---
{% include codepen.html %}
_includes/codepen.html
{% if page.codepen %}
{% assign c = page.codepen %}
<div id="codepen">
<button class="btn" onclick="document.body.classList.toggle('sticky')">Dock</button>
<iframe height="265" scrolling="no" title="{{ c.title }}"
src="//codepen.io/{{ c.author }}/embed/{{ c.id }}/?height=265&theme-id=dark&default-tab=result"
frameborder="no" allowtransparency="true" allowfullscreen="true">
See the Pen <a href='https://codepen.io/{{ c.author }}/pen/{{ c.id }}/'>in browser sql terminal</a> by {{ c.name }}
(<a href='https://codepen.io/{{ c.author }}'>#{{ c.author }}</a>) on <a href='https://codepen.io'>CodePen</a>.
</iframe>
</div>
{% endif %}
You can also use a parametrized include.
{% include codepen_param.html
author="jcubic"
name="Jakub T. Jankiewicz"
id="dVBaRm"
title="in browser sql terminal"
%}
_includes/codepen_param.html
{% assign pen = include %}
<div id="codepen">
<button class="btn" onclick="document.body.classList.toggle('sticky')">Dock</button>
<iframe height="265" scrolling="no" title="{{ pen.title }}"
src="//codepen.io/{{ pen.author }}/embed/{{ pen.id }}/?height=265&theme-id=dark&default-tab=result"
frameborder="no" allowtransparency="true" allowfullscreen="true">
See the Pen <a href='https://codepen.io/{{ pen.author }}/pen/{{ pen.id }}/'>in browser sql terminal</a> by {{ pen.name }}
(<a href='https://codepen.io/{{ pen.author }}'>#{{ pen.author }}</a>) on <a href='https://codepen.io'>CodePen</a>.
</iframe>
</div>
Like this, you can call any number of pens from your page.

Making Logo as Welcome Header in Neat Theme in Bigcartel

I am currently working on customizing the welcome page for my Bigcartel site.
I am using the Neat theme and would like to add my logo as the welcome header that appears over the slideshow, instead of just having the standard text.
The problem I am having is when I enter the code for the image, it either erases the slideshow or gives me the image with the header text still appearing.
To replace the Home page welcome text with your uploaded header image, head to Customize Design > Advanced > Home then find the following code:
{% if theme.welcome_header != blank or theme.welcome_subheader != blank %}
<div class="featured_holder">
<div class="featured">
{% if theme.welcome_subheader != blank %}<p>{{ theme.welcome_subheader }}</p>{% endif %}
{% if theme.welcome_header != blank %}<h2>{{ theme.welcome_header }}</h2>{% endif %}
Shop Now
</div>
</div>
{% endif %}
Remove that block, and replace it with the following:
<div class="featured_holder">
<div class="featured">
<img src="{{ theme.logo.url | constrain: '400' }}" alt="{{ store.name }}">
</div>
</div>

getting data out of YML

Hello Stack Overflow community,
I am trying to include a html icon on hover.
This is my code:
<div class="country">{% include icons/home/icon-america.html %}</div>
this works fine, but the icon has to be different on each hover, so i tried this:
<div class="country">{% include icons/home/icon-{{project.country}}.html %}</div>
And this is my YML:
home:
- {folder: 'thumb_1', name: 'Chaffee', text: 'Hier komt tekst over de chaffee en nog meer', link: 'chaffee.html', country: 'america'}
this is not working, is there a way to get this work?
Thanks in advance.
This is my HTML
<div class="thumb-container">
{% for project in site.data.settings.home %}
<a href="{{project.link}}" class="thumb-unit">
<div class="backPic" style="background-image: url(assets/img/home/{{ project.folder}}/thumb.jpg)"></div>
<h3>{{ project.name}}</h3>
<p>{{ project.text}}</p>
<div class="thumb-overlay"></div>
<div class="country">{% include icons/home/icon-america.html %}</div>
</a>
{% endfor %}
</div>
Wim
I solved this one by putting the svg's in a seperate folder, and linking to them like this:
{folder: 'thumb_1', country: 'america', name: 'M24 Chaffee', text: 'Hier komt tekst over de chaffee en nog meer', link: 'chaffee.html'}
and the html like this:
<img src="assets/img/home_icons/{{ project.country}}/icon.svg" alt="" />

Symfony2 - Displaying uploaded image in Twig from a Data Fixture or blog text

How do I display an image in Twig loaded from a Doctrine Fixture that I've uploaded to the web/images folder? It displays when calling it from the acutal Twig but not from fixtures or entering the line when creating the blog text.
I have tried this line which doesn't load when in a fixture file (or inside the blog body when creating the blog entry) BUT when I use this line inside of the actual Twig file it does the image correctly?
<img src="{{ asset(['images/', blog.image]|join) }}" />
Using this to display the blogs in twig:
{% autoescape false %}
<p>{{ blog.blog|truncate(2000) }}</p>
{% endautoescape %}
I think your missing a quote mark in example 2
try
changing:
<img class="alignleft" src="{{ asset('images/5375a30370200.jpeg) }}" alt="" width="290" height="275" />
to:
<img class="alignleft" src="{{ asset('/images/5375a30370200.jpeg') }}" alt="" width="290" height="275" />
Or set a new variable
{% set src = 'images/' ~ blog.image %}
<img class="alignleft" src="{{ asset( src ) }}" alt="" width="290" height="275" />
http://twig.sensiolabs.org/doc/tags/set.html
Turns out I have to use Twig's template_to_string function.

Resources