This is what I'm trying to achieve:
\documentclass{article}
\begin{document}
\add{One}
\add{Two}
\add{Three}
\print % Only here "One Two Three" will be rendered
\end{document}
How can this be done?
\documentclass{article}
\newcommand\myadds{}
\newcommand\add[1]{\expandafter\def\expandafter\myadds
\expandafter{\myadds#1 }}
\begin{document}
\add{One}
\add{Two}
\add{Three}
\myadds
\end{document}
If you want this to work even if the \adds occur in groups, then change \def to \gdef.
If you want to be able to add paragraphs as part of your \add content, then include \expandafter\long at the beginning of the \add definition.
This final, all inclusive definition seems functionally equivalent with the built-in system macro \g@addto@macro, as in
\documentclass{article}
\newcommand\myadds{}
\makeatletter
\newcommand\add[1]{\g@addto@macro\myadds{#1 }}
\makeatother
\begin{document}
\add{One}
\add{Two}
\add{Three}
\myadds
\end{document}
For example with etoolbox (\gappto appends globally, you can use \appto if you want it locally).
\documentclass{report}
\usepackage{etoolbox}
\begin{document}
\newcommand\print{}
\gappto\print{one}
\gappto\print{two}
\gappto\print{three}
and now \print
\end{document}
With the L3 programming layer (now in the LaTeX kernel).
\documentclass{article}
\ExplSyntaxOn
\tl_new:N \myadds
\NewDocumentCommand \add { m } { \tl_put_right:Nn \myadds { #1 } }
\ExplSyntaxOff
\begin{document}
\add{One}
\add{Two}
\add{Three}
\myadds
\end{document}
As an entry in the most obscure approach: This puts each entry into a separate savebox. Its only practical application is if you want to pull them apart again.
\documentclass{article}
\makeatletter
\newif\if@keepgoing
\let\mylist=@empty
\def\add#1{@next@id@freelist{}{}\sbox@id{#1}@cons\mylist@id}
\def\print{\loop
@next@id\mylist{@keepgoingtrue}{@keepgoingfalse}%
\if@keepgoing
\unhbox@id\space
@cons@freelist@id
\repeat}
\makeatother
\begin{document}
\add{One}
\add{Two}
\add{Three}
\print
\end{document}
I am not exactly sure what you are trying to achieve, so I am going to go out on a limb here. The way I handled dynamic content was by generating LaTeX code in another language—in my case that was Ruby with its ERB templates. Constant elements were in pure LaTeX, while for each accumulated item I generated appropriate LaTeX code using Ruby.
This also enables you to separate data logic from actual output format, if that is possible. However, an ERB template is used to generate the whole LaTeX file, which is then compiled into a PDF—I am not sure what motivates your question, and thus if this works for you.
Here's a sample code logic:
\documentclass{article}
\begin{document}
\begin{env}
<% $data.each do |item| -%>
\strong{item}
<% end -%>
\end{env}
\end{document}