Opened 8 months ago

Last modified 8 months ago

#5830 new change

Provide access to globals from filters

Reported by: juliandoucette Assignee:
Priority: P3 Milestone:
Module: Sitescripts Keywords:
Cc: wspee, kvas, jsonesen Blocked By:
Blocking: Platform: Unknown / Cross platform
Ready: yes Confidential: no
Tester: Unknown Verified working: no
Review URL(s):

Description (last modified by kvas)

Background

I can't define a global in globals and access it in filters. It's also not possible to access globals from other globals, which leads to code duplication sometimes.

What to change

I want to be able to access globals from filters and other globals. Example (only showing filter usage):

globals/example.py

example = 'example'

filters/examplify

def examplify(str):
  return '%s (%s)' % (str, example)

includes/example

<h1>{{ "My test" | examplify }}</h1>

output

<h1>My test (example)</h1>

Change History (4)

comment:1 Changed 8 months ago by juliandoucette

Real world example: The language selector on the new abp.org contains a location code after the language name e.g. "English (US)". We already have a filter called to_og_locale which matches crowdin language codes to ISO locale codes (which include location codes). If I could define og_locales as a global then I could write a filter to provide *just* a location code (instead of a full locale) from a crowdin language. e.g.

def to_og_location(crowdin_language):
  if (crowdin_language in og_locales)
    return og_locales[crowdin_language].split('_')[-1]
  else
    return crowdin_language.split('_')[-1]

(I used two filters to solve this problem today e.g. locale | to_og_locale | to_og_location where to_og_location just blindly returns the split('_')[-1] of what it was given.)

Last edited 8 months ago by juliandoucette (previous) (diff)

comment:2 follow-up: Changed 8 months ago by kvas

What do you think about globals being accessible from other globals? It seems to me like this would be a reasonable piece of functionality to add to this ticket, and it doesn't seem to make it much harder, so I'd be interested to know if you find it a useful addition?

comment:3 in reply to: ↑ 2 Changed 8 months ago by juliandoucette

Replying to kvas:

I think that it would be very useful to be able to access globals in globals. Please add that functionality to this ticket too if it is not too much trouble.

comment:4 Changed 8 months ago by kvas

  • Description modified (diff)
  • Priority changed from Unknown to P3
  • Ready set
Note: See TracTickets for help on using tickets.