This code allows you to call any view that returns with render_to_string via a tag like this: {% callview package.module1.module2.viewname arg1=value1 arg2=value2 ... %} The view's context will contain the arguments passed through the tag.
1 from django import template
2
3 register = template.Library()
4
5 @register.tag
6 def callview(parser, token):
7 bits = str(token.contents).split()
8 modulename, viewname = bits[1].rsplit('.', 1)
9 if len(bits) < 2:
10 raise TemplateSyntaxError, "callview tag takes at least 2 arguments"
11 module = __import__(modulename, globals(), locals(), [viewname])
12 view = getattr(module, viewname)
13 return CallViewNode(view, bits[2:])
14
15 class CallViewNode(template.Node):
16 def __init__(self, view, args):
17 self.view = view
18 self.args = args
19
20 def render(self, context):
21 for arg in self.args:
22 key, value = arg.split('=')
23 context[key] = value
24 return self.view(context)
Return true for match, else false Retourn true en cas d'egalité sinon false
1 public static bool compare<T>(ICollection<T> tabA, ICollection<T> tabB) where T : IComparable
2 {
3 System.Diagnostics.Debug.Assert(tabA != null);
4 System.Diagnostics.Debug.Assert(tabB != null);
5
6 if ( tabA.Count != tabB.Count)
7 return false;
8
9 IEnumerator<T> enumA = tabA.GetEnumerator();
10 IEnumerator<T> enumB = tabB.GetEnumerator();
11 while ( enumA.MoveNext() && enumB.MoveNext() )
12 {
13 if ( enumA.Current.CompareTo(enumB.Current) != 0 )
14 return false;
15 }
16 return true;
17 }
simple xhtml template for erb
1 template = [
2 '<!DOCTYPE html',
3 'PUBLIC "-//W3C/DTD XHTML 1.0 Strict//EN"',
4 'http://www.w3.org/TRxhtml1/DTD/xhtml1-strict.dtd">',
5 '<html>',
6 "<head>",
7 "<title><%= html_title %></title>",
8 "</head>",
9 "<body>",
10 "<%= html_body %>",
11 "</body>",
12 "</html>"
13 ]
14
15 template = ERB.new( template.join("\n") )
Singleton template inplementation
1 public class Singleton<T> where T : new()
2 {
3 static T _instance = default(T);
4
5 protected Singleton()
6 {
7 }
8
9 public static T Instance
10 {
11 get
12 {
13 if (_instance == null)
14 {
15 _instance = new T();
16 }
17 return _instance;
18 }
19 }
20 }
21
22 public class MySingleton : Singleton<MySingleton>
23 {
24 public void hello() { Debug.WriteLine("Hello"); }
25 }
Pages : 1