{"id":10,"date":"2022-02-09T20:25:37","date_gmt":"2022-02-09T20:25:37","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/rudb2\/?p=10"},"modified":"2022-02-09T20:25:37","modified_gmt":"2022-02-09T20:25:37","slug":"what-is-a-declarative-web-framework-anyway","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/rudb2\/2022\/02\/09\/what-is-a-declarative-web-framework-anyway\/","title":{"rendered":"What is a declarative web framework anyway?"},"content":{"rendered":"\n<p>One of my first tasks during my software engineering internship was to evaluate and select a frontend framework to use for a suite of web applications that the company planned to build.  The extent of my web development experience up to this point had been creating simple websites with a little bit of JavaScript embedded, but I only had a vague idea of what a web framework even was.  So I started where any good intern would, with some well crafted Google searches like &#8220;what is a web framework?&#8221; and &#8220;which framework is best?&#8221;<\/p>\n\n\n\n<p>After learning more about front end frameworks, I narrowed the choice down to Vue.js and React.  It seemed like these were both frameworks that had been widely adopted and had a solid base of documentation, third party add-ons and libraries, and active user communities.  Both Vue.js and React touted that they took advantage of &#8220;declarative rendering&#8221;, which sounded great, but I had no idea what that meant and why it was a good thing, so in this blog post I will describe what declarative rendering is and how it can help you as a programmer.<\/p>\n\n\n\n<p>From <a href=\"https:\/\/en.wikipedia.org\/wiki\/Declarative_programming\">Wikipedia<\/a>, declarative programming is &#8220;a non-imperative style of programming in which programs describe their desired results without explicitly listing commands or steps that must be performed.&#8221; In a declarative web framework, you can create a template describing what the web page should look like, and if the state of the application changes, the page is re-rendered to reflect those changes without you having to write code to explicitly update the DOM. <\/p>\n\n\n\n<p>For example, let&#8217;s say we have a simple web page that has a &lt;p&gt; element that displays the number of times a button on the page has been clicked.  If we wanted to implement this in an imperative manner, we would attach a listener to the button.  If the button is pressed, the listener calls a function to increase the value inside the &lt;p&gt; by one and re-render the page.  <\/p>\n\n\n\n<p>If we implemented this page using a declarative manner using Vue.js it might look something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;\n  &lt;p&gt;{{ count }}&lt;\/p&gt;\n  &lt;button @submit=\"increment\"&gt;Increment&lt;\/button&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\n    export default {\n        data(){\n            return {\n                count: 0\n            }\n        },\n        methods: {\n            increment: function (){\n                this.count += 1;\n            }\n        }\n    }\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>If you are not familiar with Vue, the template contains the HTML\/CSS describing what the page should look like along with some Vue directives.  The double curly brackets indicate that the value of the count variable should be rendered, and @submit=&#8221;increment&#8221; means when the button is pressed, the increment method will be called.  The script section defines variables and functions.  In the declarative model, we define a template of what the page should look like.  We tell the browser to make a &lt;p&gt; tag that displays the value of count.  When count is incremented, Vue will work behind the scenes to ensure that the page is re-rendered with the updated value of count.  <\/p>\n\n\n\n<p>This is a simple example of declarative rendering, but it can be very powerful and once you get the hang of it, your life becomes much easier as a programmer because you don&#8217;t have to focus your efforts on explicit instructions to update the DOM.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of my first tasks during my software engineering internship was to evaluate and select a frontend framework to use for a suite of web applications that the company planned to build. The extent of my web development experience up to this point had been creating simple websites with a little bit of JavaScript embedded,&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.oregonstate.edu\/rudb2\/2022\/02\/09\/what-is-a-declarative-web-framework-anyway\/\">Continue reading <span class=\"screen-reader-text\">What is a declarative web framework anyway?<\/span><\/a><\/p>\n","protected":false},"author":11930,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/rudb2\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/rudb2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/rudb2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/rudb2\/wp-json\/wp\/v2\/users\/11930"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/rudb2\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/rudb2\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":12,"href":"https:\/\/blogs.oregonstate.edu\/rudb2\/wp-json\/wp\/v2\/posts\/10\/revisions\/12"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/rudb2\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/rudb2\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/rudb2\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}