Template tags and partials

So the first big difference I’m running into between the Old Way (Django) and the New Way (Rails), is that Django uses a templating tool like handlebars or mustache (I’m not sure how to tell which from what I’m seeing in the code here), which has the nice effect of really forcing the developer to separate the logic from the view.

Note: Django’s pattern looks more like Model-View-Template in practice, because what a Raisld eveloper would call a Controller, Django calls a “View” which then renders a template, rather than a Rails Controller/Action rendering a View Template.

Using Django templatetags

The challenge is, that in order to force this strong separation of concerns, when a developer (me) wants to render something more complex in Django, it’s necessary to define a custom ‘tag‘ or a ‘filter‘ that the templating engine can then load and use. In the world of “basically all programming languages do the same thing“, this is really pretty equivalent in outcome to a Rails ‘partial‘, though the latter doesn’t offer any guardrails to prevent the commingling of logic and display, which can be a problem for debugging. But a sawzall doesn’t prevent one from cutting through a power line either… doesn’t make it less awesome of a tool. The user just needs to be cognizant of what they’re doing.

Defining a Django templatetag

So I think the first “big” “refactor” for Galactic Impact will be to take these templatetags that I built to render things like Stars or Ships on the galaxy map.

When I draw a star on the galaxy map, I need to know a few things about it based on the active player (current_user in Rails login parlance):

Have they explored it by sending a ship there? If not, it renders as a generic “star” icon, and they can’t click into it to see what planets are there. But if they have ever sent a ship there, they can click in and examine each of the system’s planets, or dispatch ships directly to those planets, rather than just heading to the system.

If they have either settlers or a capital there, we want to ring the Star icon with an indicator to let them know that.

And finally, if an enemy empire has any established presence in the system, we want to badge the planet with a box to let them know there might be trouble if they send ships there.

The old system’s “templatetag” was a pure-python function which took a User and a Star as parameters, and it would do the work to figure out what should render, and output that into the templating engine. It was a pretty straightforward HTML copy into a Rails partial, and it turned out pretty seamlessly.

So now I’ve got a page that spits out a bunch of stars by rendering the collection through the partial, and includes rendering all the extra badges and whatever that let the player understand the galaxy from a ‘war room’ perspective.

“Proxima Centuri” rendered via a Rails partial.

Leave a Comment

Your email address will not be published. Required fields are marked *