garter/urls.html @ 9d47cefb26fd

flask-csrf: Update documentation.
author Steve Losh <steve@stevelosh.com>
date Tue, 11 May 2010 20:14:52 -0400
parents 388fd65c29fc
children (none)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
    "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
          xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
          xmlns:dc="http://purl.org/dc/elements/1.1/"
          xmlns:foaf="http://xmlns.com/foaf/0.1/">
      
      <head>
        
            
            
                <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
            
            
            <title>
                Garter ยป
                Urls
            </title>
            
            
                <!-- YUI CSS reset, fonts, base -->
                <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?3.0.0/build/cssreset/reset-min.css&amp;3.0.0/build/cssfonts/fonts-min.css&amp;3.0.0/build/cssbase/base-min.css" media="screen, projection" />
                
                <link rel="stylesheet" type="text/css" href="media/css/style.css" media="screen, projection" />
                <link rel="stylesheet" type="text/css" href="media/css/pygments.css" media="screen, projection" />
                <link rel="stylesheet" type="text/css" href="media/css/garter.css" media="screen, projection" />
            
            
            
            
        
    </head>
    
    <body >
        
            
                
                    
  
    <ol id="breadcrumbs">
      
        <li class="crumb-0 not-last">
          
            <a href="./">index</a>
          
        </li>
      
        <li class="crumb-1 last">
          
            urls
          
        </li>
      
    </ol> <!-- ol#breadcrumbs -->
  

                
            
            
            <div id="content">
                
                
                
                <h1><a href="">URL Convenience Functions</a></h1>

<p>URLs are a pain. Garter tries to help by providing some useful functions to
make working with URLs easier.</p>
<div class="toc">
<ul>
<li><a href="#permalink">permalink</a></li>
</ul>
</div>
<h2 id="permalink">permalink</h2>
<p>The <code>permalink</code> decorator was taken from <a href="http://flask.pocoo.org/snippets/6/">this snippet</a> on the
Flask site. It's used to wrap functions so they only need to return the
arguments to Flask's <code>url_for</code> function, instead of calling the function
themselves.</p>
<p>For example, say you have several classes that represents items on your site:</p>
<div class="codehilite"><pre><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">url_for</span>

<span class="k">class</span> <span class="nc">Event</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">event_id</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">event_id</span> <span class="o">=</span> <span class="n">event_id</span>

    <span class="k">def</span> <span class="nf">link</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">url_for</span><span class="p">(</span><span class="s">&#39;event&#39;</span><span class="p">,</span> <span class="n">event_id</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">event_id</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">username</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="o">=</span> <span class="n">username</span>

    <span class="k">def</span> <span class="nf">link</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">url_for</span><span class="p">(</span><span class="s">&#39;profile&#39;</span><span class="p">,</span> <span class="n">username</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">username</span><span class="p">)</span>
</pre></div>


<p>Using the permalink decorator can make the <code>link</code> functions a bit cleaner:</p>
<div class="codehilite"><pre><span class="kn">from</span> <span class="nn">garter.urls</span> <span class="kn">import</span> <span class="n">permalink</span>

<span class="k">class</span> <span class="nc">Event</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">event_id</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">event_id</span> <span class="o">=</span> <span class="n">event_id</span>

    <span class="nd">@permalink</span>
    <span class="k">def</span> <span class="nf">link</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;event&#39;</span><span class="p">,</span> <span class="p">{</span> <span class="s">&#39;event_id&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">event_id</span> <span class="p">}</span>

<span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">username</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="o">=</span> <span class="n">username</span>

    <span class="nd">@permalink</span>
    <span class="k">def</span> <span class="nf">link</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;profile&#39;</span><span class="p">,</span> <span class="p">{</span> <span class="s">&#39;username&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="p">}</span>
</pre></div>
                
                
                
                
                <hr class="clear" />
            </div>
            
            
                <div id="footer">
                    <p>
                        <a href="http://sjl.bitbucket.org/garter/">Garter</a>
                        was lovingly crafted by
                        <a href="http://stevelosh.com/">Steve Losh</a>.
                        The documentation is powered by
                        <a href="http://markdoc.org/">Markdoc</a>.
                    </p>
                </div>
            
        
        
        <hr class="clear" />
    </body>
</html>