how to write python code in the zope page template - plone

I am very new to Zope and Plone. I am trying to write python code in the index_html page. I have the list of objects of type person, now I want to reorder them. So, what I had before was:
<ul tal:define="persons python: context.portal_catalog(portal_type='Person');">
<tal:listing repeat="p persons">
Now I have this python code before the <tal:listing...
<?python
order=[0,2,1]
persons = [persons[i] for i in order]
?>
But somehow the order of the person remains the same. Also, I also don't like this way of writing python code in the view. Is there any way I could use this code for changing the order of the list?

Zope pagetemplates do not support a <? ?> syntax at all.
However, you can loop over your python list in the tal:repeat just fine:
<ul tal:define="persons python: context.portal_catalog(portal_type='Person');">
<tal:listing repeat="i python:[0, 2, 1]">
<li tal:define="p python:persons[i]" tal:content="p/name">Person name</li>
</tal:listing>
</ul>
I suspect however, that you want to let the portal_catalog do the sorting instead, using the sort_on parameter (see the Plone KB article on the catalog):
<ul tal:define="persons python: context.portal_catalog(portal_type='Person', sort_on='sortable_title');">
<tal:listing repeat="p persons">
<li tal:content="p/name">Person name</li>
</tal:listing>
</ul>
If you want to do anything more complex, use a browser view to do the list massaging for you.

Related

Is it possible to pass class to binding attribute in Angular?

We have a solution based on a numeric field being set to 1 or 2 or displaying certain icons. It works.
<span *ngIf="config.icon"
[class.fas]="true"
[class.fa-plus]="icon===1"
[class.fa-minus]="icon===2"
class="indicator">
</span>
I sense what shortly, we'll have a whole bunch of such icons (about 15 different ones). The suggested solution is either to put in 15 lines of specific class assignments or to build a specialized component managing that.
I'm opposed to both but haven't got it working out when I tried and googling led to irrelevant hits. Possibly due to my incompetence recognized th egood stuff.
Is it possible to do something like one of the following pseudo code lines? How?
[class.fa-{{iconName}}]="true"
[class]="iconName"
edit
Based on the comments/answers, I got it working using the following.
<span *ngIf="config.icon"
[ngClass]='{ "fas": true, "fa-plus": true }'></span>
However, for some reason, I'm not getting anything using the syntax below.
<span *ngIf="config.icon"
[ngClass]="classes"></span>
...
classes: { "fas": true, "fa-plus": true };
What am I missing?
You can try something like this:
<span class= "indicator"
[class.fas]="true"
[ngClass]="'fa-' + iconName">
</span>
See working example at https://stackblitz.com/edit/angular-mgecjw

POEdit doesn't extract string in HTML tags

I'm having a problem with Laravel's blade templating syntax.
When having something like:
<input placeholder="{{ __('My Tooltip') }}" />
that string won't be founded by POEdit. But on same file if I had this:
<span>{{ __('My Tooltip') }}</span>
that's OK.
I've already added a new extractor with follow configs but the problem persists.
Command: xgettext --language=Python --add-comments=TRANSLATORS: --force-po -o %o %C %K %F
An item in keywords list: -k%k
An item in input files list: %f
Source code charset: --from-code=%c
Anyone can help me?
You lie to Poedit about the content of these files, pretending they are Python, even when they are very clearly not. It’s not at all surprising that it doesn’t work to your liking. What would be surprising would be if it did. In this case, the reason seems to be clear: xgettext’s Python parser, naively trusting you and hopelessly confused as the result, sees the " in there as a start of a string literal not prefixed with __ and so skips over it.
Fix it by doing what the documentation says: use a dedicated tool to extract the string. In laravel-gettext's case that means using this command:
php artisan gettext:update
(Upcoming Poedit 2.0 will have direct support for template languages like this, but until then, you need to use the CLI tools.)

Aptana 3 Code Folding

Ive started using Aptana 3 today and really like it,
However I'm struggling to find out if I can code fold to specific levels.
For example I can push Ctrl+Shift+Divide and will collapse EVERYTHING imaginable.
Including the class.
lets just say my doc is as follows:
class Kill_model extends Game_Model{
function shoot(){
//code
//code
//code
//code
//code
}
function respawn(){
//code
//code
//code
//code
//code
}
function spectate(){
//code
//code
//code
//code
//code
}
}
The default will collapse to
class Kill_model extends Game_Model{}
I've been using PHPEdit in the past, and like to "Fold to Level 2"
This gives me the appearance of
class Kill_model extends Game_Model{
function shoot(){}
function respawn(){}
function spectate(){}
}
I was wondering if its possible to just fold down to level 2, by level 2 I assume it means 2 levels deep. Level 1 = Class, level 2 = functions within.
Many thanks.
Ok, so level folding is available in Aptana, it just isn't built into PHP editing, only Source editing. To add it to PHP, you can go to Commands > Source > Edit this Bundle, and the Commands > PHP > Edit this Bundle and copy Source/commands/folding.rb to PHP/commands/folding.rb (this will be a new file). If you do not have Option and Command keys (Mac, I believe), you will want to change the keybindings in this file to something else, like Control and Alt. You will find the keybinding in the folding.rb file looking something like this:
with_defaults :input => :none, :output => :discard, :key_binding => "OPTION+COMMAND+0" do
and a second time like this:
cmd.key_binding = "CONTROL+ALT+" + level.to_s
Just change the OPTION to CONTROL and the COMMAND to ALT, and you will have a new Ctrl+Alt+ shortcut once you restart Aptana.
See my second answer for more direct info... I thought I'd leave this one in case it helps someone with a similar but not quite the same problem...
I can't speak for the keyboard shortcut because I don't know where numpad_divide is on my laptop (no numpad) - but if you look under Window > Preferences > Aptana Studio > Editors > PHP, you can choose to initially fold "these elements" - if you check "Functions" I think you may get the folding you are looking for. However, I do not think this preference will affect the behavior of Ctrl+Shift+Divide aka Collapse All.
There is also a command to collapse the current block (Ctrl+Numpad_minus) but I think this would be less useful to you.
You may also find the Quick Outline helpful, if you are looking for a short overview of the available classes and functions in your file. This can be accessed with Ctrl+O (or right click > Quick Outline).
Edit: Playing around with Aptana today I found, under Commands > Source > Folding > Toggle Foldings at Level > Level <x>. There appears to be a shortcut associated with each level, Alt+1, Alt+2, etc. but it doesn't work for me. I also don't see an option to configure a shortcut for these commands, but you can theoretically make your own.

Drupal 7: print field to region in node--type.tpl.php file

In Drupal 6, it was so easy to print content to an existing region using the following:
<?php
drupal_set_content('left-sidebar', $field_MY_FIELD_rendered);
?>
I've been searching and searching for how to do this in Drupal 7 after finding out drupal_add_region_content() doesn't work and I'm at a loss.
I can get the exact output I'm looking for using $output = render($content['field_MY_FIELD']);, but I see no way to output that to a block without having to write a hook in a separate module or some such thing.
Any ideas on how to output $output to a region using node--type.tpl.php?
Thanks in advance,
Dan
Have you tried putting field--field_myfield.tpl.php in your templates directory?

ReST strikethrough

Is it possible to strike text through in Restructured Text?
Something that for example renders as a <strike> tag when converted to HTML, like:
ReSTructuredText
I checked the docs better, as suggested by Ville Säävuori, and I decided to add the strikethrough like this:
.. role:: strike
:class: strike
In the document, this can be applied as follows:
:strike:`This text is crossed out`
Then in my css file I have an entry:
.strike {
text-decoration: line-through;
}
There is at least three ways of doing it:
.. role:: strike
An example of :strike:`strike through text`.
.. container:: strike
Here the full block of test is striked through.
An undecorated paragraph.
.. class:: strike
This paragraph too is is striked through.
.. admonition:: cancelled
:class: strike
I strike through cancelled text.
After applying rst2html you get:
<p>An example of <span class="strike">strike through text</span>.</p>
<div class="strike container">
Here the full block of test is striked through.</div>
<p>An undecorated paragraph.</p>
<p class="strike">This paragraph too is is striked through.</p>
<div class="strike admonition">
<p class="first admonition-title">cancelled</p>
<p class="last">I strike through cancelled text.</p>
You use them with a style
.strike {
text-decoration: line-through;
}
Here I have taken the admonition directive as example but any
directive that allow the :class: option would do.
As it generates a span the role directive is the only one that
allow to apply your style to a part of a paragraph.
It is redundant to add a class strike to a directive also named
strike, as suggest Gozzilli, because the directive name is the default
class for the html output.
I have checked these syntax both with rest2html and Sphinx. But
while everything works as expected with rest2html the class
directive fail with Sphinx. You have to replace it with
.. rst-class:: strike
This paragraph too is is striked through.
This is only stated in a small
footnote of Sphinx reSt Primer.
According to the official spec there is no directive for strikethrough markup in ReST.
However, if the environment allows for :raw: role or you are able to write your own roles, then you can write a custom plugin for it.
I found the other answers very helpful.
I am not very familiar with Sphinx but I am using it for a project. I too wanted the strike-through ability and have got it working based on the previous answers.
To be clear, I added my strikethrough role as gozzilli mentioned but I saved it inside my conf.py using the rst_prolog variable as discussed in the stack overflow thread here. This means that this role is available to all of your rest files.
I then extended the base html template as described above by creating layout.htmlwithin _templatesinside my source directory. The contents of this file are:
{% extends "!layout.html" %}
{% set css_files = css_files + ["_static/myStyle.css"] %}
This basically includes a custom css file to all your built default html docs.
Finally, in my _static directory within my source directory I included the file myStyle.css which contains:
.strike {
text-decoration: line-through;
}
Which the other answers have already provided.
I am merely writing this answer as it wasn't obvious to me with my limited Sphinx experience which files to edit.
Here's a Python definition of a del role, which works better than the accepted answer if you want to use the role in multiple pages of a Pelican blog or a Sphinx documentation project:
from docutils import nodes
from docutils.parsers.rst import roles
def deleted_role(_role, rawtext, text, _lineno, _inliner, options={}, _content=[]):
roles.set_classes(options)
options.setdefault('classes', []).append("del")
return [nodes.inline(rawtext, text, **options)], []
roles.register_canonical_role('del', deleted_role)
Even better would be to extend the HTML writer to produce a proper <del> tag, like this:
from docutils import nodes
from docutils.parsers.rst import roles
from docutils.writers._html_base import HTMLTranslator
class delnode(nodes.inline):
pass
def visit_delnode(self, node):
self.body.append(self.starttag(node, 'del', ''))
def depart_delnode(self, node):
self.body.append('</del>')
HTMLTranslator.visit_delnode = visit_delnode
HTMLTranslator.depart_delnode = depart_delnode
def deleted_role(_role, rawtext, text, _lineno, _inliner, options={}, _content=[]):
roles.set_classes(options)
return [delnode(rawtext, text, **options)], []
roles.register_canonical_role('del', deleted_role)
You can trivially adjust it to produce an <s>, of course.
Consider the user may have a different background, so here is no one solution that can be suitable for everyone.
1.Only one file
If you only use it only on one file. For example, you published a simple project to PyPI, and you may probably just only one README.rst file. The following may you want.
.. |ss| raw:: html
<strike>
.. |se| raw:: html
</strike>
single line
=============
|ss| abc\ |se|\defg
multiple line
=============
|ss|
line 1
line 2
|se|
789
you can copy and paste it on this website: https://livesphinx.herokuapp.com/
and will see the picture as the following:
It's simple, and you can on directly see the preview on some IDE, for example, PyCharm.
bellow is writing for the users of Sphinx
2.beginner of Sphinx
If you are a beginner of Sphinx. ( I mean maybe you want to use Sphinx to create a document, but Python is not familiar for you ) then try as following:
# conf.py
from pathlib import Path
html_static_path = ['_static', ]
html_css_files = ['css/user.define.css'] # If you want to control which HTML should contain it, you can put it on the HTML, which is very like the answer by #Gregory Kuhn.
with open(Path(__file__).parent / Path('_static/css/user.define.rst'), 'r') as f:
user_define_role = f.read()
rst_prolog = '\n'.join([ user_define_role + '\n',]) # will be included at the beginning of every source file that is read.
# rst_epilog = '\n'.join([ user_define_role + '\n',]) # it's ok if you put it on the end.
user.define.rst
.. role:: strike
user.define.css
.strike {text-decoration: line-through;}
With the rst_prolog, It can auto-add the role on each rst files, but if you change the content( that file, it contains a format that you define), then you must rebuild to make the render is correct.
3.Create roles
You can create an extension to achieve it.
# conf.py
extensions = ['_ext.rst_roles', ]
html_static_path = ['_static', ]
html_css_files = ['css/user.define.css']
# rst_roles.py
from sphinx.application import Sphinx
from docutils.parsers.rst import roles
from docutils import nodes
from docutils.parsers.rst.states import Inliner
def strike_role(role, rawtext, text, lineno, inliner: Inliner, options={}, content=[]):
your_css_strike_name = 'strike'
return nodes.inline(rawtext, text, **dict(classes=[your_css_strike_name])), []
def setup(app: Sphinx):
roles.register_canonical_role('my-strike', strike_role) # usage: :my-strike:`content ...`
The full architecture:
conf.py
_ext/
rst_roles.py
_static/
css/
user.define.css
about the rules, you can reference this link rst-roles
And I vary recommended you to see the docutils.parsers.rst.roles.py .
I wrote an extension for this.
Just pip install sphinxnotes-strike and use:
:strike:`text`
or
:del:`text`
to show strike text.
For more info: https://sphinx-notes.github.io/strike/
Since Docutils 0.17, the HTML5-writer uses <del> if a matching class value is found in inline, literal, or container elements:
.. role:: del
:del:`This text has been deleted`, here is the rest of the paragraph.
.. container:: del
This paragraph has been deleted.

Resources