Skip to content

Commit

Permalink
documented jockes new bindings feature
Browse files Browse the repository at this point in the history
git-svn-id: https://erlyaws.svn.sourceforge.net/svnroot/erlyaws/trunk/yaws@582 9fbdc01b-0d2c-0410-bfb7-fb27d70d8b52
  • Loading branch information
Claes Wikstrom committed Jan 27, 2004
1 parent a9a9971 commit 379666b
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 8 deletions.
9 changes: 9 additions & 0 deletions man/yaws_api.5
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,15 @@ The {ssi, File, Delimiter, Bindings} statement can also
occur inside a deep ehtml structure.


.TP
\fB{bindings, [{Key1, Value2}, {Key2, Value2} .....]}\fR
Establish variable bindings that can be used in the page.

All bindings can then be used in the rest of yaws code
(in HTML source and within erl tags).
In HTML source %%Key%% is expanded to Value and within erl
tags yaws_api:get_binding(Key) can be used to extract Value.

.TP
\fB[ListOfValues]\fR
It is possible to return a deep list of the above defined
Expand Down
10 changes: 6 additions & 4 deletions src/yaws_compile.erl
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ compile_file(C, LineNo, Chars = "</erl>" ++ Tail, erl, NumChars, Ack, Es) ->
%% this is boring but does actually happen
%% in order to get proper user errors here we need to catch i/o
%% or hack compiler/parser
yaws:elog("Dynamic compile error in file ~s, line ~w~n~s",
[C#comp.infile, LineNo, Str]),
yaws:elog("Dynamic compile error in file ~s (~s), line ~w~n~s",
[C#comp.infile, C#comp.outfile,LineNo, Str]),
A2 = {error, NumChars, ?F("<pre> Dynamic compile error in file "
" ~s line ~w~n~s </pre>",
[C#comp.infile, LineNo, Str])},
Expand Down Expand Up @@ -262,9 +262,11 @@ comp_err(C, _LineNo, NumChars, Err) ->
[{_FileName, [ {Line0, Mod, E} |_]} |_] when integer(Line0) ->
Line = Line0 + C#comp.startline - 10,
?Debug("XX ~p~n", [{_LineNo, Line0}]),
Str = io_lib:format("~s:~w:~n ~s\n",
Str = io_lib:format("~s:~w:~n ~s\ngenerated file at: ~s~n",
[C#comp.infile, Line,
apply(Mod, format_error, [E])]),
apply(Mod, format_error, [E]),
C#comp.outfile
]),
HtmlStr = ?F("~n<pre>~nDynamic compile error: ~s~n</pre>~n",
[Str]),
yaws:elog("Dynamic compiler err ~s", [Str]),
Expand Down
9 changes: 9 additions & 0 deletions src/yaws_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,15 @@ handle_method_result(Res, CliSock, IP, GS, SC, Req, H, Num) ->
erase(post_parse),
erase(query_parse),
erase(outh),
lists:foreach(fun(X) ->
case X of
{binding, _} ->
erase(X);
_ ->
ok
end
end, get()),

aloop(CliSock, GS, Num+1);
done ->
maybe_access_log(IP, SC, Req, H),
Expand Down
2 changes: 2 additions & 0 deletions www/EXHEAD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<td> <A HREF="/form.yaws">Forms</a> </td>
<td> <A HREF="/redirect.yaws">Redirect</a> </td>
<td> <A HREF="/ssi.yaws">Server side include</a> </td>
<td> <A HREF="/bindings.yaws">Bindings</a> </td>


</tr>
<tr border="2">
Expand Down
115 changes: 115 additions & 0 deletions www/bindings.yaws
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<erl>
out(A) ->
[{ssi, "HEAD", [],[]},
{ssi, "EXHEAD", [],[]}].
</erl>


<p>
Bindings is the opposite of <a href="ssi.yaws"> SSI, Server Side Includes</a>,
SSI is primarily used when basicaly the entire pages are written in EHTML and
snippets of HTML, or more typical, javascript code is inserted into the EHTML
code.

<p> Bindings are used the other way around, basically the entire
pages are written in regular HTML but parts of the HTML needs to be
dynamically generated.

<p>The yaws callback out/1 can return

<div class="box">
<pre>
{bindings, [{Key1, Value2}, {Key2, Value2} .....]}.
</pre>
</div>


<p>All bindings can then be used in the rest of yaws code (in HTML source and
within erl tags). In HTML source %%Key%% is expanded to Value and
within erl tags yaws_api:get_binding(Key) can be used to extract Value.

<p>With the binding feature it is easier to write transparant yaws code making
it easier to to work together with Web people knowing little or
nothing about Erlang.

An example:


<div class="box">
<pre>

&lt;erl&gt;
out(A) -&gt; {bindings, [{"A", "foo"}, {"B", "baz"}]}.
&lt;/erl&gt;

&lt;html&gt;
&lt;body&gt;
&lt;p&gt;%%A%%&lt;/p&gt;
&lt;p&gt;&lt;font size="4"&gt;%%A%% != %%B%%&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;An enormous amount of plain html source here.&lt;/p&gt;

&lt;erl&gt;
out(A) -&gt;
Value = yaws_api:binding("A"),
{ehtml, {ul, [],
[{li, [],
Value},
{li, [],
"gazonk"}]}}.
&lt;/erl&gt;

%%A%% = %%A%% (hit me)
&lt;/body&gt;
&lt;/html&gt;
</pre>
</div>


<p>
Which expands to:

<div class="box">
<pre>



&lt;html&gt;
&lt;body&gt;
&lt;p&gt;foo&lt;/p&gt;
&lt;p&gt;&lt;font size="4"&gt;foo != baz&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;An enormous amount of plain html source here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;foo&lt;/li&gt;
&lt;li&gt;gazonk&lt;/li&gt;&lt;/ul&gt;

foo = foo (hit me)



</pre>
</div>


And is rendered as:
<div class="box">
<p>foo</p>
<p><font size="4">foo != baz</font></p>
<p>An enormous amount of plain html source here.</p>

<ul>
<li>foo</li>
<li>gazonk</li></ul>

foo = foo (hit me)
</div>





</html>




2 changes: 1 addition & 1 deletion www/man.yaws
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ out(A) ->
end
end, Page) of
true ->
os:cmd("man " ++ Page ++ " | col -b");
os:cmd("man " ++ Page ++ " | col -b -p -x");
false ->
"illegal character detected in query arg"
end;
Expand Down
7 changes: 4 additions & 3 deletions www/redirect.yaws
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta name="keywords" content="post">


<TITLE>Top EE page</TITLE>
<TITLE>Redirect example</TITLE>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

Expand All @@ -16,12 +16,12 @@

<H1> redirect.yaws </H1>

<p> This a redirection example, the following <a href="redirect2.yaws">redirection url</a> contains the following code in its first <erl> group.
<p> This a redirection example, the following <a href="redirect2.yaws">redirection url</a> contains the following code in its first erl group.


<erl>

out(_) -> yaws_api:pre_ssi_string(
out(_) -> 1 yaws_api:pre_ssi_string(
"
out(_) ->
yaws_api:redirect(
Expand All @@ -37,3 +37,4 @@ as well as a Location header.

</HTML>


0 comments on commit 379666b

Please sign in to comment.