Our Gemara on Amud Beis tells us the famous story of Shimon Hatzaddik’s encounter with a pious and sincere Nazir. Shimon Hatzaddik kisses the young man on his head, apparently in the courtyard of the Beis HaMikdash.
The problem with this is that it seems to contradict a ruling of the Rama (OC 98:1):
It is forbidden for a person to kiss one's small* children in synagogue, in order to fix in one's heart that there is no love like the love of the Omnipresent Who is Blessed. ]
(Some understand this as referring to only small children, thus the question regading Shimon Hatzaddik does not begin. However, though Rama uses the word “small” in his commentary on Shulkan Arukh, in Darchei Moshe 98:3 he does not use that modifier.)
Before we try to answer this question, we should take a moment to understand better what might seem to be a cruel ruling. This is not about ignoring a young child’s anguish. A very young child shouldn’t be in Shul in the first place. Secondly, if it was a child of the age of chinuch in shulo and he got hurt and needs comfort, many poskim hold that one can kiss or hug the child, as it is not an act of frivolity but an act of healing. ( Click on this article to download ) The Rama’s ruling is about maintaining solemnity during prayers and to not engage in frivolous or playful behavior in Shul.
In fact, the concern may be so related to solemnity and prayer that it is reported from numerous sources that Rav Moshe would kiss his grandchildren in Shul, just not during prayers ( see prior link ). Rav Moshe’s argument was that Rama consciously chose to place this ruling in the section of Shulkhan Arukh that discusses honoring and preparing for prayer (98) instead of the section that discusses the laws of maintaining sanctity in Shul (151).
Others disagree with Rav Moshe’s ruling though our Gemara is an excellent proof to his opinion. If the prohibition is only during prayer, we now have no problem with Shimon Hatzaddik kissing the Nazir in the Beis HaMikdash.