Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""Renderer.""" 

2from pkg_resources import resource_filename 

3 

4# Deform 

5import deform 

6import deform.form 

7 

8 

9def configure_zpt_renderer(search_path=(), translator=None): 

10 """Initialize ZPT widget rendering for Deform forms. 

11 

12 Include given package asset paths in the paths Deform uses to 

13 look up widgets. 

14 

15 Example: 

16 

17 .. code-block:: python 

18 

19 from pyramid.threadlocal import get_current_request 

20 

21 # 

22 # Set up Chameleon templates (ZTP) rendering paths 

23 # 

24 

25 def translator(term): 

26 # i18n localizing function 

27 return get_localizer(get_current_request()).translate(term) 

28 

29 # Configure renderer 

30 configure_zpt_renderer(("deformdemo:custom_widgets",), translator) 

31 

32 

33 :param search_path: List of additional search paths for widget templates. 

34 

35 :param translator: Translator function to localizing i18n strings 

36 """ 

37 

38 # Don't let the user to slip in a string 

39 assert type(search_path) in (tuple, list) 

40 

41 # Add more paths to besides the default one 

42 default_paths = deform.form.Form.default_renderer.loader.search_path 

43 paths = [] 

44 for path in search_path: 

45 pkg, resource_name = path.split(":") 

46 paths.append(resource_filename(pkg, resource_name)) 

47 

48 deform.form.Form.default_renderer = deform.ZPTRendererFactory( 

49 tuple(paths) + default_paths, translator=translator 

50 )