Wrap Phalcon flash messages in HTML element - phalcon

Is there a possibility to wrap flash messages in an element? I want to have no html element at all when there are no messages and have an extra div containing all messages if there is any message.
It would be enough if I could at least get information whether there are any flash messages and then code it myself, but it seems to me that neither Phalcon\Flash\Direct nor Phalcon\Flash\Session allow you to access current message count or wrap messages in your own html element.

Just configure your flash service to just output the message:
$this->flash->setAutomaticHtml(false);
Also, when outputting a message, it's automatically echoed.
If you want to just return a string without echoing it to the output buffer use:
$this->flash->setImplicitFlush(false);
These methods aren't in the main documentation page, but you should always look at the class reference too, you might find very usefull information there :)
EDIT
To return only messages you use setAutomaticHtml to false, setImplicitFlush has nothing to do with it. Also to know if a message exists use something like this:
$this->flashSession->has('error');

I have ended with following code. I basically had to generate output myself.
<?php
$messages = $this->flashSession->getMessages();
if ( count($messages) > 0) {
?>
<div class="basic-bg">
<div class="main-column">
<div class="flash-messages">
<?php
foreach ($messages as $messageType => $messageArray) {
foreach ($messageArray as $message) {
echo "<div class=\"flash-$messageType\">$message</div>";
}
}
?>
</div>
</div>
</div>
<?php } ?>

I know this is an old thread, but how about implementing an extended class to make sure your message string is still correctly escaped?
This is the class I've used to implement Bootstrap 3 dismissable messages:
<?php
namespace Ext;
/**
* Extension to Phalcon Framework to implement Bootstrap 3 dismissable messages.
* Pass mappings of phalcon to bootstrap classes to construct
* #link https://docs.phalconphp.com/uk/latest/reference/flash.html Phalcon flash docs
* #author Kevin Andrews <kevin#zvps.uk>
*/
class FlashBootstrap extends \Phalcon\Flash\Session
{
/**
* Correctly escapes the message while building a Bootstrap 3
* compatible dismissable message with surrounding html.
* #param string $type
* #param string $message
* #return void
*/
public function message($type, $message)
{
$bootstrapCssClass = $this->_cssClasses[$type];
$errorType = ucfirst($type);
$bootstrapMessage = "<div class=\"alert alert-{$bootstrapCssClass} alert-dismissible\" role=\"alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">×</span></button><strong>{$errorType}:</strong> {$this->getEscaperService()->escapeHtml($message)}</div>";
parent::message($type, $bootstrapMessage);
}
}
Also for completeness the initialisation for DI:
<?php
$di->set('flash', function() {
$bootstrapFlash = new Ext\FlashBootstrap(array(
'error' => 'alert alert-danger alert-dismissible',
'success' => 'alert alert-success alert-dismissible',
'notice' => 'alert alert-info alert-dismissible',
'warning' => 'alert alert-dismissible',
));
$bootstrapFlash->setAutoescape(false);
$bootstrapFlash->setAutomaticHtml(false);
return $bootstrapFlash;
});
This also has the advantage that the ->success() ->error() ->notice() and ->warning() helper methods will all go through this code and produce nicely formatted messages wrapped in the desired HTML.

{% if flash.has('notice')==true OR flash.has('success') %}
{% for notif in flash.getMessages('success') %}
<div class="notif_global success">
<div class="notif_global-title">Успешно</div>
<div class="notif_global-content">{{ notif }}</div>
<div class="notif_global-close ico_close"></div>
</div>
{% endfor %}
{% for notif in flash.getMessages('notice') %}
<div class="notif_global success">
<div class="notif_global-title">Сообщение</div>
<div class="notif_global-content">{{ notif }}</div>
<div class="notif_global-close ico_close"></div>
</div>
{% endfor %}
{% endif %}
{% if flash.has('warning')==true OR flash.has('error') %}
{% for notif in flash.getMessages('warning') %}
<div class="notif_global error">
<div class="notif_global-title">Предупреждение</div>
<div class="notif_global-content">{{ notif }}</div>
<div class="notif_global-close ico_close"></div>
</div>
{% endfor %}
{% for notif in flash.getMessages('error') %}
<div class="notif_global error">
<div class="notif_global-title">Ошибка</div>
<div class="notif_global-content">{{ notif }}</div>
<div class="notif_global-close ico_close"></div>
</div>
{% endfor %}
{% endif %}

Related

'size' on custom product metafield returns '0'

I'm building a component which checks the character count of a custom product metafield; if it's above 24 characters, the data is output as an accordion, otherwise the metafield content is printed in its entirety. The conditional below fails as the metafield size always returns 0, but I can see the content printing via the else statement so I'm certain the path is valid:
{% if product.metafields.custom.product_note.size >= 24 %}
<div class="product-note has--dropdown">
<span class="item__heading item__trigger">Product information</span>
<div class="item__content">
{{ product.metafields.custom.product_note }}
</div>
</div>
{% else %}
<div class="product-note">
<div class="item__content">
{{ product.metafields.custom.product_note }}
</div>
</div>
{% endif %}
I'm not sure it's relevant, but the product_note metafield is a multi-line text field. If anyone could point me in the right direction as to why size is failing to produce a value, I'd appreciate it massively.
You could always try working off the value contained in the metafield. It appears you are short-cutting by referring to just the combo of namespace and key, without actually saying: what is the length of the value stored at that namespace and key. Just a thought. You could at least try that.
Final answer, courtesy of #David Lazar's suggestion:
{% assign data_product_note = product.metafields.custom.product_note.value %}
{% if data_product_note.size >= 24 %}
<div class="product-note has--dropdown">
<span class="item__heading item__trigger">Product information</span>
<div class="item__content">
{{ data_product_note }}
</div>
</div>
{% else %}
<div class="product-note">
<div class="item__content">
{{ data_product_note }}
</div>
</div>
{% endif %}

Shopify metafield newline_to_br adds line break on anchor tag

I've added a multi-line metafield to my collections so that I can insert a long description into my template. The code works well, but if there's an anchor tag within the long_description, a line break is added before and after it. Is there a means to stop that from happening?
<div class="content-indent">
<div class="row tt-layout-promo-box">
<h3>About Our {{ collection.title }}</h3>
{{ collection.metafields.my_fields.long_description | newline_to_br }}
</div>
</div>{%- endif -%}```
<script>
let parser = new DOMParser();
let str = document.querySelector('.tt-layout-promo-box h3');
let stringValue = str.textContent;
str.innerHTML = parser.parseFromString(stringValue, 'text/html').body.innerHTML;
</script>

Can not display my comment lists for a post

i have tried to add and display comments list in my django post_detail template yet its not displaying...
This is my comment models...
class Comment(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, on_delete=models.CASCADE)
content = models.TextField()
active = models.BooleanField(default=True)
created = models.DateTimeField(blank=True, auto_now=False, auto_now_add=True)
class Meta:
ordering = ['created']
def __str__(self):
return "Comment by {} on {}".format(self.author, self.content)
i then added the comment model into my post module as follows...
from comments.models import Comment
class Post(models.Model):
comment = models.ForeignKey(Comment, blank=True, null=True)
Here is my post views.py
def post_detail(request, slug):
post = get_object_or_404(Post, slug=slug)
return render(request, "blog/post_detail.html", {
'post':post,
})
comments = post.comment.filter(active=True)
if request.method == 'POST':
#A comment was posted
comment_form = CommentForm(request.POST or None)
if comment_form.is_valid() and user.is_authenticated():
#Create new comment but dont save to DataBase first
new_comment = comment_form.save(commit = False)
#Assigns the current instance to the comment
new_comment.post = post
#Saves the comment to DataBase
new_comment.save()
else:
comment_form = CommentForm()
return render(request, "blog/post_detail.html", {
'post':post,
'comments': comments,
'comment_form': comment_form
})
finally i tried to display these into my template yet i couldn't get the list of comments for each post
<!-- displays available comments for this post -->
{% for comment in comments %}
<div class="media d-block d-md-flex">
<img class="d-flex rounded-circle avatar z-depth-1-half mb-3 mx-auto" src="https://mdbootstrap.com/img/Others/documentation/img (2)-mini.jpg" alt="Avatar">
<div class="media-body text-center text-md-left ml-md-3 ml-0">
<h5 class="mt-0 font-weight-bold blue-text">comment {{ forloop.counter }} by {{ comment.author }} {{ comment.created }}</h5>
{{ comment.content|linebreaks }}
</div>
</div>
{% endfor %}
</div>
</div>
<!--/.Comments-->
{% endif %}
<!-- Displays comment form for registered users else link visitors to registeration page -->
{% if user.is_authenticated %}
{% if new_comment %}
<h4><span class="badge badge-pill green">your comment has been added<i class="fa fa-check" aria-hidden="true"></i></span></h4>
{% else %}
<form method="POST">
{% csrf_token %}
<div class="form-group shadow-textarea">
<label for="exampleFormControlTextarea6">Add a comment</label>
<textarea class="form-control z-depth-1" id="exampleFormControlTextarea6" rows="3" placeholder="Write something here...">
{{ comment_form.as_p }}
</textarea>
</div>
<div class="text-center mt-4">
<button class="btn btn-info btn-md" type="submit" value="add comment">submit</button>
</div>
</form>
{% endif %}
{% else %}
<h4>You have to register in order to comment this post </h4>
{% endif %}
please i need help getting this to display
it have eaten up my day.
I finally found the error...
i was rendering the post_detail template before the comment_form
here is my final code
def post_detail(request, slug):
post = get_object_or_404(Post, slug=slug)
comments = Comment.object.filter(active=True)
if request.method == 'POST':
#A comment was posted
comment_form = CommentForm(request.POST or None)
if comment_form.is_valid() and user.is_authenticated():
#Create new comment but dont save to DataBase first
new_comment = comment_form.save(commit = False)
#Assigns the current instance to the comment
new_comment.post = post
#Saves the comment to DataBase
new_comment.save()
else:
comment_form = CommentForm()
return render(request, "blog/post_detail.html", {
'post':post,
'comments': comments,
'comment_form': comment_form
})

Phalcon assign view variable inconsistent

Having a problem getting consistent behavior assigning variables to the view. For example:
In controller:
$this->view->media = Media::findFirst(['groupId=0', 'order' => 'RAND()', 'limit' => 1]);
In view:
{% if media is defined %}
<div class="thumbnail">
<img src="{{ static_url('img/media/thumbs/' ~ media.name) }}" class="img-round">
<div class="caption">
<h3>{{ media.title }}</h3>
<p>{{ media.description }}</p>
</div>
</div>
{% endif %}
Which is parsed like:
<?php if (isset($media)) { ?>
<div class="thumbnail">
<img src="<?php echo $this->url->getStatic('img/media/thumbs/' . $this->media->name); ?>" class="img-round">
<div class="caption">
<h3><?php echo $this->media->title; ?></h3>
<p><?php echo $this->media->description; ?></p>
</div>
</div>
<?php } ?>
The problem is that when the parsed version of the template, $media is not accessible via $this so the isset($media) passes, but the references to $this->media returns nothing.
Any way to force $media to be local in scope?
I think I got it.
Misbehaviour description
You have probably declared a media module in your DI(). I was trying quite a lot to reproduce that error, and got it finally when i set a dumb media service among configuration files:
$di->set('media', function() {
return new \stdClass();
});
and than got the same behavior you have - Volt during compilation is not sure, what variable to use and choses $this->media (DI::get('media')) instead of $media or $this->view->media var for obtaining data.
Solution
If you dont want to resign from calling you findFirst result under that variable name, you may bypass that by using View in volt directly:
{% if view.media is defined %}
<div class="thumbnail">
<img src="{{ static_url('img/media/thumbs/' ~ view.media.name) }}" class="img-round">
<div class="caption">
<h3>{{ view.media.title }}</h3>
<p>{{ view.media.description }}</p>
</div>
</div>
{% endif %}
it will generate $this->view->media calls instead of $this->media ones.
+1 on that question.

How do i dynamically/evaluate via expression json property in django template

reading in a JSON for sport. using a partial for matchup markup.
awayteam and home team for most part share identical markup but the JSON properties which I have no control are such below:
<div class="away {{game.awayTeam_last_name|slugify}}">
<a href="#" title="{{game.awayTeam_first_name}} {{game.awayTeam_last_name}}">
<span class="{{league}}-logo"></span>
<span class="city">{{game.awayTeam_first_name}}</span>
{% if game.event_status != "pre-event" %}
<span title="score">{{game.awayTeam_score}}</span>
{% else %}
<span title="record entering game">(0-0)</span>
{% endif %}
</a>
</div>
<span>#</span>
<div class="home {{game.homeTeam_last_name|slugify}}">
<a href="#" title="{{game.homeTeam_first_name}} {{game.homeTeam_last_name}}">
<span class="{{league}}-logo"></span>
<span class="city">{{game.homeTeam_first_name}}</span>
{% if game.event_status != "pre-event" %}
<span title="score">{{game.homeTeam_score}}</span>
{% else %}
<span title="record entering game">(0-0)</span>
{% endif %}
</a>
</div>
is there a way to shrink/refactor the above like some expression valuator to make home and away passed via a variable.
didn't answer own question but i did get Data guys to better format the data, so became..
awayteam: { first_name:'', last_name:'', score:'' }
hometeam: { first_name:'', last_name:'', score:'' }
allowing me to shrink in half the template =)