
take a look at templating languages like
handlebars or the templating language
that is built into view for example and
I’m going to compare the templating
languages to a native Jaso coach where
you were JavaScript plays the role of
the templating language this is the
approach that react uses for instance
when you’re advocating for using a
templating language you often cite two
main benefits that templating languages
are expressive and declarative I’m going
to talk about those benefits but I am
also going to talk about three costs
that come with using templating
languages templating languages add
complexity to your stack they add a
social cost and they result in a lot of
drooling that is what we’re going to
talk about today I am mpj and you are
watching fun on a function if we look
back in time say male 5 years ago or so
the industry standard way of rendering
Dom was to use a templating language to
glue your business logic together with
your Dom then a couple of years ago I do
you remember how old react is like three
four years anyway reacts arrived on the
scene and uh it did away with templating
languages instead it used just native
JavaScript in playing the role that
templating languages had before I just
want to be clear here that we’re not
concerned with JSX or the virtual Dom in
this video now JSX
is a thin layer on top of the native
native javascript that react uses and
we’re not really concerned with that
we’re concerned with the fact that it’s
it’s it’s still native javascript are
under that very very thin layer it’s not
consequential and also under
reiax doesn’t technically generate real
doumitt generates a virtual dominant
bits that it like we’re not really
concerned with that either in this video
what we’re talking about here is like
the pure custom templating language and
we’re comparing that to the native Jas
equivalent the native j as a pro ties
been gaining a lot of traction in recent
years but templating languages we’ve
used them for a long time and a lot of
developers are not convinced that the
native Jas is a is a step forward in
this video I’m going to talk a little
bit about the cost and benefits of
templating languages and give you like a
sense of thinking behind the native JX
uses and why they have moved away from
templating languages and why a lot of
people do it expressive where templating
languages are very expressive because
unlike JavaScript that is a
general-purpose programming language
templating languages are designed to do
just one thing and that is to render
HTML declarative templating languages
are designed to encourage declarative
interfaces by the way if you’re not sure
what declarative programming is you
might want to check out this video that
I made I and this video is going to make
us much more sense all technologies have
some benefits and all technologies has
some cost the first cause of templating
languages is complexity because you are
simply adding an entirely language to
your technology stack the second cost is
social cost anyone working in your
codebase now has to learn this little
extra language in addition to JavaScript
and the third cost is lots of tools if
you’re using a templating language
you’re throwing away all the power of
JavaScript not just the language itself
but also the ecosystem a lot of
libraries written in it and all the
tools and limiters and editors and ID’s
those are the costs and benefits I’m
going to be talking about today and well
let’s dive into each one so
expressiveness templating languages are
very very
pressive because they are unlike
JavaScript they are designed to do just
one thing and that is Express the
rendering of HTML templates are
essentially a domain-specific language
and they other things it’s like they’re
in the same category as sequel or you
know regular expressions they are very
custom languages designed to express a
very very specific thing in a specific
domain from here on out I’m going to
show you a bunch of examples I’m going
to show examples of templating languages
compared to equivalent examples in the
native JSF roach
I am very intentionally not going to be
using any real live technology here like
react or view or handlebars and the
reason for that is that a lot of the
points that I’m going to try to make or
related to familiarity and
understandability and it will just be
skewed if you are familiar with the
syntax of technology already but you can
sort all think about this example as
kind of react without JSX and kind of
handlebars and we’re going to assume
that both of them has the same rendering
traits so like imagine that both of them
are rendering a virtual Dom and all the
rendering characteristics are the same
because those are you have
implementation details we’re not
concerned with that in this video we’re
concerned with the general form of a
templating language versus a native j/s
approach all right we’re going to now
look at the code of the templating
language and remember that we are now
for now looking at this from an
expressiveness standpoint I’m going to
be silent for 15 seconds to allow you to
look at this example try to understand
all right so let’s go through it the
template has this dim it’s called the
prices container and this in turn it
contains this unordered list with some
some items here note that it only shows
the list if it has some items in the
list otherwise it will show a paragraph
here where the tax new items found
inside inside every item
it shows the price and it also runs it
through some kind of format currency
transformation you know this is probably
yes to convert this from whatever this
is probably an into some nice currency
string I would like to draw your
attention to one thing in this example
and that is the fact that you could
probably by just looking at this figure
out what it did even though this is the
language that does not exist you are
you’re not not familiar with it upfront
even if you were not even familiar with
the temp concept of templating languages
this would probably still make sense it
doesn’t try to be a general-purpose
language like Java Script and let allows
you to you throw away a lot of power and
a lot of features in order to gain this
readability and expressiveness for this
very specific case HTML rendering and
now we’re going to look at another
example that does exactly the same thing
but it does it using a native JSA and
that looks like this I’m going to be
silent again for 15 seconds and let you
all right so let’s walk through this
this is render prices container it
returns me and you did and it it has the
CSS class list container and this is a
great example of of how a templating
language is extremely expressive because
here with JavaScript we have to use
class name because javascript is not
designed just for this it’s a
general-purpose programming language so
it has the concept of a clause is that’s
another thing JavaScript so that’s the
reserved word that we can’t use in this
context so we have to use the property
class name instead to make this work
inside of the div and it if the items
array has a length it will return a ul
unordered list and that unordered list
inside of itself has a list of of list
items it just Maps through the list
items and it returns a list item for
everyone as it yes like it template
examples it formats the currency before
putting it into the Li and if item
book-length is false e0 it will return
this paragraph with text new light and
sound so all of this is just JavaScript
there are there’s no special language
additions that we’ve done here it’s just
pure JavaScript this look a lot less
like HTML than the template example even
though it is JavaScript that we are
familiar with it still feels a little
bit alien and convoluted because we have
to sort of simulate the HTML rendering
in our hands we have to spend a little
bit more brainpower to figure out how
what what HTML this results in here is a
bit more like we asked to remove these
from our heads and then
have the HTML let’s respect the
processing that we have to do in our
head is slightly slightly less now I
would say that this is a not a huge
difference you might even actually call
this a small difference this is this is
absolutely not rocket surgery but we
still have to give it to templating
languages here that they are expressive
they are meant to express this thing
HTML rendering and they do it better
than JavaScript natively does let’s move
on and talk about declarative Ness if we
look at both of these examples they very
clearly dictate what is being rendered
neither of these examples is very
concerned with how more specifically
there is no imperative programming going
on here there are no variables being
assigned there’s no state being mutated
it’s just declaring what things are so
both of these examples are declarative
as opposed to imperative and that is a
very good thing because declarative code
is almost always easier to reason about
them imperative code and that holds
especially true and when it comes to
rendering in addition to being
declarative there’s also very little
logic going on in them they are very
easy to reason about there’s not a lot
of stuff this is true for both examples
but the key difference here between the
templating language and the native j’f
example is that the templating language
has to be this way there is no way to do
imperative programming in the templating
language in the JavaScript example we
could do all kinds of crazy things
access global variables start
maintaining state like but in the
templating language you you can’t do
that you are you cannot create variables
and stuff like that and in the
templating language there there simply
isn’t a lot of language constructor in
general so your code tends to be pretty
very minimal because there’s yes
not a lot of programming you can do and
this is by design this is the intent of
templating languages templating language
or trying to encourage even enforce
really a minimal and declarative view
templating languages are trying to
remove the ability from the programmer
to do imperative or elaborate things in
the new while I buy the whole
expressiveness say I
I really don’t buy when people talk
about templating languages as
encouraging declarative ax C but I feel
there is a problem with that line of
reasoning let’s think about this for a
minute
you have we have this problem our abuse
contain too much imperative and
elaborate logic we feel like this should
be in in the business logic instead so
we can try to solve this by introducing
an entire little language into our stack
where you can’t do imperative an
elaborate programming oh we can you stop
doing imperative and elaborate
programming in our views yes stop doing
it just don’t put imperative elaborate
logic in your views yes stop that is a
much simpler solution than introducing
an entire little language to your tool
chain in response to that you might say
that but what about the junior
developers they don’t they have not
fully grasped the concept of separation
of concerns yet they think having a
templating language might be good like
training wheels sort of thing to keep
them from doing damage to our project
but I don’t really buy that argument
because you still have the capability to
do dumb things when it comes to
separation you have just moved it one
little layer away from the template if
you have a team member that doesn’t
understand separation of concerns no
amount of technical safeguards or going
to help you what you need to do is to
help and educate that person or you’re
going to have a big
problem on your hands so templating
languages are built to encourage
declarative Nessun views but I think a
lot less convoluted solution is to just
write simple and declarative code in
JavaScript rather than dragging an
entire little language into your stack
to prevent you from making the mistake
and this listen to the first cost of the
templating languages complexity no
matter how you cut it a template
language is a programming language that
is a reasonably big complicated thing to
pull into your project your project as a
whole is now more complex because a
templating language isn’t really the
kind of component that removes a lot of
complexity so you sacrifice some
simplicity in order to get the
templating language now it should be
said that this is a personal value
statement in the sense that I am a
person that values simplicity very very
high like probably probably higher than
most people I once or twice gotten the
feedback that I should accept more
complexity into solutions there are
absolutely developers that do not mind
this extra complexity they feel that
adding a templating language to your
project that’s not a big deal that’s not
a big component they feel it’s well used
it’s popular it’s free it’s maintained
by a third party regularly what’s the
big deal but that’s not how I view it I
view it as like a thousand tiny cuts
kind of thing my philosophy is that the
more things thingies that you add to
your project or whatever in life really
the more things you add that can go
wrong the more things will go wrong when
we add a new tool to our tool chain that
will add complexity and that might be
okay I really must stress that it might
be ok like we need to be able to absorb
some complexity of course because we
need to you know solve problems and they
are
problems are complex and in a very real
manner we need to have tools and we need
to absorb some complexity but the point
I’m trying to make is that when you add
something it must pay for its own
complexity cost so a templating language
might not be the biggest complexity cost
in the world but the benefits that a
templating language brings in I think
that they are very small and I think
that the complexity trade-off the return
of investment here is a bad deal and
complexity is only one of the costs of
of templating languages there’s also a
social cost I talked before about the
fact that a templating language is a
domain-specific language a dsl other
languages in that family is sequel which
is used to describe data access and
regular expressions which is used to
describe the string pattern matching
they are the same family but there is an
important distinction and that is that
regular expressions and sequel or
relatively standardized but templating
languages are not like that there are
many different ones so new developers
coming into your project are not very
likely to know the particular one that
you are using so they are going to have
to spend some time learning it and
learning how to work with it let’s go
back to the original template example
now as we concluded before this is very
expressive so it’s easy to figure out
what it does it’s very easy to to read
and understand that but that does not
mean that we have understood the
language that we know how to read it
doesn’t mean that we know how to write
it let’s say that you had the assignment
that we want wanted to pad the price
with zeroes if it’s less than 10 let’s
say that we already have the function
it’s pad price with zeroes or or
something like that how do we get that
in here I could probably figure out that
it might be something like this and but
I don’t really know how to act
we get the function into the system I
need to figure out how to register this
function in in the templating parser and
you probably need to do some kind of
plugin thing or something and this is
only because we’re lucky because we have
this example here like if we imagine
that this was just the thing and we we
have not seen this this syntax before
and templating languages they’re a bit
sneaky this way because they are easy to
read they feel approachable but that
feeling goes away once you start trying
to write them then you realize that they
are not really as intuitive as they
first clean and you might say oh what’s
the big deal
developers you just hit the docs find it
google it developers do this all the
time it’s not rocket surgery your
developer what’s the big deal well let’s
have a look at the the same problem in
the native jsy brer ii and since this is
yes javascript and we know javascript we
can just well add the function done we
don’t need to learn any plug-in system
we just include the file it’s just
normal javascript we didn’t need to
learn anything we could just make the
change straight away so yeah it’s not
rocket surgery to look up some dogs and
add some plugin but it’s a problem that
only exists because of the templating
language a native jeaious library does
not have this problem you don’t have it
you don’t do that work a template
language has a higher social cost than a
native jace library so what template
language has a complexity cost a social
cost and want to talk about a third
clause and that is loss of tools
javascript is a very mature language
very fast and mature engines and
incredibly vast wealth of libraries many
mature editors and i des and a lot of
code analysis tools when you develop in
another language you do
access to all of that and you are
limited to whatever ecosystem is for
that specific language for example let’s
have a look at the native jsr berry
example again I can paste this just into
my editor and I have access to
everything that that whatever
fancy-pants bells I have for my
JavaScript development environment code
highlighting linting contextual
autocomplete stuff like that I mean if I
change here it immediately figures that
something is wrong if I do the same
thing here like it has no idea because
it is a string there might of course be
plugins and stuff for this templating
language that you can install but you’ve
now added one extra thing for your
developers to install and that tooling
is also very unlikely to be as mature as
their JavaScript equivalents you also
lose access to the power of the language
itself template languages are limited in
fact this is by design like we talked
about before it’s to keep you from doing
elaborate things in your rendering but
there are things that are not possible
to do in the templating language but are
still simple and minimalist enough to
want to have in there let’s say for
instance that we wanted to I don’t know
calculate the total of the items let’s
in a templating language you would
probably not be allowed to do a sum
operation if you’re not familiar with
reduce this example is probably gonna
make you what is this if there is an
episode here on reduce it’s very simple
it’s used to sum things often but the
point in an inner templating language
you are probably not allowed to do a sum
operation that’s that’s something that
is the philosophy of a templating
language is to relegate to to outside
the template it’s a bit of an academic
discussion if sum of items is business
logic or view logic I think it
definitely can qualify as view logic
others might disagree I don’t know what
do you think comment if you do agree
that summing items is view logic then
you can see how a template language
makes your life a little bit harder
another thing that JavaScript gives you
for free is composition for example
let’s say that I had different types of
items that could be in the list so I
want to really use the list view
container so to speak but I want to be
able to shove different kinds of items
in there with different kinds of
templates have a look at this and try to
first wrap your head around it on your
all right so let’s have a look at the
example regular list it’s more or less
the same function from before but we
have given it a general name and we have
modified it to accept a render item
function here we also render item is now
the thing that goes into the matter so
if you remember the example needs to
look like this before but we have now
broken it out into these and these
variables so in order to create a
function that is equivalent to what we
had before we use bind to Curry render
price list and what that means is that
we call bind on render list and we call
bind with render price item as an
argument and that will return a new
function from bind to render prices
where render price item has been fixed
as the first argument of of rendered is
it’s kind of permanent I stood so render
price list it’s now just called with
items this has now been fixed as the
first argument if currying is a new
concept to you I have an episode on that
here yeah car so don’t dismiss this as
weird because you don’t know what
carrying is because carrying is a very
thing and you should learn so notice how
all of this is just plain JavaScript the
div ul and @p things here that they’re
the same from before but notice that
there is no concept of a component being
introduced here and this works because
we’re doing everything with just plain
functions plain functions are simple and
composable this composability that we
get completely for free here needs to be
reimplemented in a templating language
often in the form of component
components or great react does have them
but when working with react you can also
get away with just using simple render
function and that is nicer because it is
yes much simpler and those were my
thoughts in summary template languages
are good at expressing HTML rendering
that
what they are built for template
languages aim to encourage minimalistic
and declarative programming but you can
just do that anyway in JavaScript the
cost of templating languages is added
stack complexity social cost and lots of
tools you have just watched an episode
of fun fun function I release this every
Monday morning always hundred GMT if you
don’t want to wait until next Monday you
can watch this episode that’s the brain
in a jar that Google brands as machine
learning has selected for you I am mvj
until next Monday morning stay curious